Home > Software design >  Adding to GET the same endpoint with POST and different Queries ends up with inconsistent error mess
Adding to GET the same endpoint with POST and different Queries ends up with inconsistent error mess

Time:12-07

When adding the same routes with different methods, and per method queries the response of a get call is different, however since the other method is POST it should be unaffected.

With POST: Playground: https://go.dev/play/p/xzoAkpEhGgy


// You can edit this code!
// Click here and start typing.
package main

import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"time"

    "github.com/gorilla/mux"

)

func main() {

    r := mux.NewRouter()
    
    r.HandleFunc("/api/v2", func(w http.ResponseWriter, r *http.Request) {
        // an example API handler
        fmt.Fprintf(w, "You made a POST request")
        json.NewEncoder(w).Encode(map[string]bool{"ok": true})
    }).Methods("POST")
    
    r.HandleFunc("/api/v2", func(w http.ResponseWriter, r *http.Request) {
        // an example API handler
        fmt.Fprintf(w, "You made a GET request")
        json.NewEncoder(w).Encode(map[string]bool{"ok": true})
    }).
        Queries("from", "{from:[0-9] }",
            "to", "{to:[0-9] }").Methods("GET")
    
    
    srv := &http.Server{
        Handler: r,
        Addr:    "127.0.0.1:8000",
        // Good practice: enforce timeouts for servers you create!
        WriteTimeout: 15 * time.Second,
        ReadTimeout:  15 * time.Second,
    }
    go srv.ListenAndServe()
    
    
    req2 := httptest.NewRequest("GET", "/api/v2?from=3&to=-5", nil)
    out2 := httptest.NewRecorder()
    
    r.ServeHTTP(out2, req2)
    
    fmt.Println(out2.Code)
    fmt.Println(out2)

}

Expected 404, got 405

Whilst removing POST playground: https://go.dev/play/p/EXiF00_WrFW

// You can edit this code!
// Click here and start typing.
package main

import (
    "encoding/json"
    "fmt"
    "net/http"
    "net/http/httptest"
    "time"

    "github.com/gorilla/mux"
)

func main() {

    r := mux.NewRouter()

    r.HandleFunc("/api/v2", func(w http.ResponseWriter, r *http.Request) {
        // an example API handler
        fmt.Fprintf(w, "You made a GET request")
        json.NewEncoder(w).Encode(map[string]bool{"ok": true})
    }).
        Queries("from", "{from:[0-9] }",
            "to", "{to:[0-9] }").Methods("GET")


    srv := &http.Server{
        Handler: r,
        Addr:    "127.0.0.1:8000",
        // Good practice: enforce timeouts for servers you create!
        WriteTimeout: 15 * time.Second,
        ReadTimeout:  15 * time.Second,
    }
    go srv.ListenAndServe()


    req2 := httptest.NewRequest("GET", "/api/v2?from=3&to=-5", nil)
    out2 := httptest.NewRecorder()

    r.ServeHTTP(out2, req2)

    fmt.Println(out2.Code)

}

Results in 404

For GET request the routing and result should be consistent. 404-s

I am curious if anyone seen this problem before?

CodePudding user response:

Router will try to find a match based on path and query parameters. The GET route is not matched because of query string parameters not matching the requirements.

However the path matches the POST route because that route doesn't care about those query parameters. Then the request method is checked and it doesn't match the route so 405 Method Not Allowed is returned (there is a handler for that route, but for different method).

You can get the desired behavior by adding catch-all GET handler for the same path:

// You can edit this code!
// Click here and start typing.
package main

import (
    "encoding/json"
    "fmt"
    "net/http"
    "net/http/httptest"
    "time"

    "github.com/gorilla/mux"
)

func main() {

    r := mux.NewRouter()

    r.HandleFunc("/api/v2", func(w http.ResponseWriter, r *http.Request) {
        // an example API handler
        fmt.Fprintf(w, "You made a POST request")
        json.NewEncoder(w).Encode(map[string]bool{"ok": true})
    }).Methods("POST")

    r.HandleFunc("/api/v2", func(w http.ResponseWriter, r *http.Request) {
        // an example API handler
        fmt.Fprintf(w, "You made a GET request")
        json.NewEncoder(w).Encode(map[string]bool{"ok": true})
    }).
        Queries("from", "{from:[0-9] }",
            "to", "{to:[0-9] }").
        Methods("GET")
    r.HandleFunc("/api/v2", func(w http.ResponseWriter, r *http.Request) {
        http.Error(w, "", http.StatusNotFound)
    }).Methods("GET")

    srv := &http.Server{
        Handler: r,
        Addr:    "127.0.0.1:8000",
        // Good practice: enforce timeouts for servers you create!
        WriteTimeout: 15 * time.Second,
        ReadTimeout:  15 * time.Second,
    }
    go srv.ListenAndServe()

    req2 := httptest.NewRequest("GET", "/api/v2?from=3&to=-5", nil)
    out2 := httptest.NewRecorder()

    r.ServeHTTP(out2, req2)

    fmt.Println(out2.Code)

}

  • Related