Search code examples
restgomuxgorilla

What Response Code to return on a non-supported HTTP Method on REST?


I'm using Gorilla Mux to handle my HTTP routes for my REST API. I'm using the following binding for my API:

r := mux.NewRouter()
r.Host(baseUrl)

api := r.PathPrefix("/api").Subrouter()
api.HandleFunc("/users", APIUsers).Methods("GET", "POST")

http.Handle("/", r)
http.ListenAndServe(":8083", nil)

Then I do a CURL like this:

$ curl -i -X PUT http://127.0.0.1:8083/api/users

The Method APIUsers doesn't get called, as expected. I do get a 200 OK back tho:

HTTP/1.1 200 OK
Date: Tue, 30 Dec 2014 19:03:59 GMT
Content-Length: 0
Content-Type: text/plain; charset=utf-8

Should I do this different with Gorilla to, let's say, bind all failing functions to a designated function? Also I wonder what's the typical HTTP code to return on non-supported functions. I'm a assuming a code in the 400 range, but I'm not sure which to use. I don't want it to be 200 tho, as the request was not "OK".

Any ideas and tips on this subject?


Solution

  • You can set a custom NotFoundHandler but that will apply to all unmatched routes.

    If you want a specific response returned you have to define the route explicitly.

    Example:

    func main() {
        r := mux.NewRouter().PathPrefix("/api").Subrouter()
    
        // custom not found handler used for unmatched routes
        var notFound NotFound
        r.NotFoundHandler = notFound
    
        r.HandleFunc("/users", APIUsers).Methods("GET", "POST")
    
        // return 405 for PUT, PATCH and DELETE
        r.HandleFunc("/users", status(405, "GET", "POST")).Methods("PUT", "PATCH", "DELETE")
    
        http.Handle("/", r)
    
        http.ListenAndServe(":8083", nil)
    }
    
    type NotFound func(w http.ResponseWriter, req *http.Request)
    
    func (NotFound) ServeHTTP(w http.ResponseWriter, req *http.Request) {
        w.WriteHeader(404)
        w.Write([]byte(`{"message": "Not Found"}`))
    }
    
    // status is used to set a specific status code
    func status(code int, allow ...string) func(w http.ResponseWriter, req *http.Request) {
        return func(w http.ResponseWriter, req *http.Request) {
            w.WriteHeader(code)
            if len(allow) > 0 {
                w.Write([]byte(`Allow: ` + strings.Join(allow, ", ")))
            }
        }
    }
    
    func APIUsers(w http.ResponseWriter, req *http.Request) {
        w.Write([]byte("hello"))
    }