Skip to content

Include Allow header with 405 response #68

Open
@alexedwards

Description

@alexedwards

When bone returns a 405 Method Not Allowed response, it would be good if it could include an Allow header detailing which methods are permitted.

I think it could perhaps be done by amending the otherMethods helper. Something like:

func (m *Mux) otherMethods(rw http.ResponseWriter, req *http.Request) bool {
	allowed := []string{}
	for _, met := range method {
		if met != req.Method {
			for _, r := range m.Routes[met] {
				ok := r.exists(rw, req)
				if ok {
					allowed = append(allowed, r.Method)
				}
			}
		}
	}
	if len(allowed) > 0 {
		rw.Header()["Allow"] = allowed
		rw.WriteHeader(http.StatusMethodNotAllowed)
		return true
	}
	return false
}

Also, what are your thoughts on including a plain text "Method Not Allowed" message in the response body, similar to the "404 page not allowed" body for 404s?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions