Search code examples
restgomux

Routing incoming request


I am trying to create a simple API using Go that performs certain operations depending on the data provided. I was planning to provide JSON data to this API and get details from it for further use.

Since I was trying to provide JSON data I created the routing using gorilla/mux as below:

    router.HandleFunc("/msa/dom/perform-factory-reset?json={jsonData}", CallGet)
    log.Fatal(http.ListenAndServe(":8080", router))

But while trying to hit the endpoint http://localhost:8080/msa/dom/perform-factory-reset?json={"vrf":"ds","ip":"45","mac":"452","method":"gfd"} I am getting 404 page not found error.

Hence I tried to change the implementation such that new routing is done as:

router.HandleFunc("/msa/dom/perform-factory-reset/json={jsonData}", CallGet)

This works absolutely fine and I am able to perform desired tasks. Could someone tell me why this is happening?


Solution

  • Is the router gorilla/mux? If so, you cannot add query parameters to path like that. You have to:

      router.Path("/msa/dom/perform-factory-reset").
     Queries("json","{jsonData}").HandlerFunc(CallGet)
    

    If it is some other router, then you still probably have to register path without the query parameters, and then get the query parameter values in the handler from the request.