HTTP request matching different endpoint - gorilla mux

174 views
Skip to first unread message

Van Fury

unread,
Sep 2, 2021, 12:13:03 PM9/2/21
to golang-nuts
Hi All,

I have the following to handler functions, DataSetsGet and RetrieveSharedData.
When make request with the URL
https://127.0.0.1:20000/nfdm-fdm/v2/shared-data, I get response from DataSetsGet handler instead of RetrieveSharedData handler function. When I take the bracket from {id} to id, I get the right response from RetrieveSharedData handler. Any help to solve this issue, my code below with omitted codes.

```
func DataSetsGet(response http.ResponseWriter, request *http.Request) {

// Data set response codes
}

func RetrieveSharedData(response http.ResponseWriter, request *http.Request) {
// Retrieve shared data response codes
}



type Route struct {
    Name        string
    Method      string
    Pattern     string
    HandlerFunc http.HandlerFunc
}

var Router = NewRouter()

type Routes []Route

func NewRouter() *mux.Router {
    router := mux.NewRouter().StrictSlash(true)
    for _, route := range routes {
        var handler http.Handler
        handler = route.HandlerFunc

        router.
            Methods(route.Method).
            Path(route.Pattern).
            Name(route.Name).
            Handler(handler)
    }

    return router
}


var routes = Routes{
    Route{
        "DataSetsGet",
        strings.ToUpper("Get"),
        "/nfdm-fdm/v2/{id}",
        DataSetsGet,
    },

    Route{
        "RetrieveSharedData",
        strings.ToUpper("Get"),
        "/nfdm-fdm/v2/shared-data",
        RetrieveSharedData,
    },

}


func main{

addr := "127.0.0.1:6060"

server := NewServer(addr)

go func() {
        err := server.ListenAndServe()
        if err != nil && err != http.ErrServerClosed {
            logger.Log.Errorf("Could not listen on %s: %v\n", addr, err)
        }
    }()
}



// Create a new server
func NewServer(ListAddr string) *http.Server {

    return &http.Server{
        Addr:         ListAddr,
        Handler:      Router,
        ReadTimeout:  5 * time.Second,
        WriteTimeout: 10 * time.Second,
        IdleTimeout:  15 * time.Second,
    }
}
```

BR
Fury

burak serdar

unread,
Sep 2, 2021, 12:18:36 PM9/2/21
to Van Fury, golang-nuts
Your paths are ambiguous.  "/nfdm-fdm/v2/shared-data" matches  "/nfdm-fdm/v2/{id}" where id=shared_data. You can use a regex for the path with {id} to exclude the "shared_data" match.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/faf2c214-3638-4b3e-b460-1a789e351defn%40googlegroups.com.

Sean Liao

unread,
Sep 2, 2021, 1:01:54 PM9/2/21
to golang-nuts
gorilla/mux tests routes in the order they're added.
You can register your shared_data route before the {id} one if you must keep the ambiguous path,
though the better option is to not have ambiguous paths in the first place.

Van Fury

unread,
Sep 2, 2021, 1:24:00 PM9/2/21
to golang-nuts
Thank you for your answer. Very helpful.
Reply all
Reply to author
Forward
0 new messages