build-an-http-server / lesson-25.md
Lesson 25 · Building responses

Return 405 for the wrong method

A path may exist but not for the method used - POST-only endpoints should reject a GET with 405, not 404. Today you tell "no such path" apart from "wrong method for this path".

The goal

Return 405 Method Not Allowed with an Allow header when a path exists but not for the requested method.

Start here - the target
TO DO
Scenario: Path exists, method does not
Givena router with only POST "/submit" registered
Whena request for GET "/submit" is dispatched
Thenthe response status is 405 and it has header "Allow: POST"
Background

A 404 says the resource does not exist; a 405 Method Not Allowed says it exists but not for the method you used — GETting an endpoint that only accepts POST, say. Distinguishing them means that after an exact method-and-path miss you check whether the path is registered under any method.

When it is, answer 405 and include an Allow header listing the methods that path does accept — the spec requires it, and clients use it to retry correctly. This is exactly why lesson 23 keyed the table on method and path together: the information you need is already there to scan.

Make it work
// exact method+path missed; is the PATH known under any method?
if allowed := m.methodsFor(r.Path); len(allowed) > 0 {
return Response{Status: 405,
Headers: map[string]string{"Allow": strings.Join(allowed, ", ")}}
}
// otherwise fall through to 404
CheckpointDONE
A known path with the wrong method returns 405 with an Allow header. Commit.