Merge pull request #11818 from hashicorp/improve-url-not-found-response

http: improve 404 Not Found response message
This commit is contained in:
Jared Kirschner 2021-12-13 16:08:50 -05:00 committed by GitHub
commit 7b78ded3c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 0 deletions

3
.changelog/11818.txt Normal file
View File

@ -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/)
```

View File

@ -587,6 +587,12 @@ func (s *HTTPHandlers) Index(resp http.ResponseWriter, req *http.Request) {
// Check if this is a non-index path // Check if this is a non-index path
if req.URL.Path != "/" { if req.URL.Path != "/" {
resp.WriteHeader(http.StatusNotFound) 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 return
} }