From 2de79abc00a2bf3f244bc5b60827c8405bc12ce0 Mon Sep 17 00:00:00 2001 From: Jared Kirschner Date: Mon, 13 Dec 2021 08:26:35 -0800 Subject: [PATCH] http: improve 404 Not Found response message When a URL path is not found, return a non-empty message with the 404 status code to help the user understand what went wrong. If the URL path was not prefixed with '/v1/', suggest that may be the cause of the problem (which is a common mistake). --- .changelog/11818.txt | 3 +++ agent/http.go | 6 ++++++ 2 files changed, 9 insertions(+) create mode 100644 .changelog/11818.txt diff --git a/.changelog/11818.txt b/.changelog/11818.txt new file mode 100644 index 0000000000..728e7360f1 --- /dev/null +++ b/.changelog/11818.txt @@ -0,0 +1,3 @@ +```release-note:improvement +http: when a URL path is not found, include a message with the 404 status code to help the user understand why (e.g., HTTP API endpoint path not prefixed with /v1/) +``` \ No newline at end of file diff --git a/agent/http.go b/agent/http.go index a1d8461d0d..e1f6e475bc 100644 --- a/agent/http.go +++ b/agent/http.go @@ -587,6 +587,12 @@ func (s *HTTPHandlers) Index(resp http.ResponseWriter, req *http.Request) { // Check if this is a non-index path if req.URL.Path != "/" { resp.WriteHeader(http.StatusNotFound) + + if strings.Contains(req.URL.Path, "/v1/") { + fmt.Fprintln(resp, "Invalid URL path: not a recognized HTTP API endpoint") + } else { + fmt.Fprintln(resp, "Invalid URL path: if attempting to use the HTTP API, ensure the path starts with '/v1/'") + } return }