14

I am working on a restful service in golang using chi. I am trying to create a route as below

r.Mount("/api/dest", router.NewDestRouter(chi.NewRouter(), destSrv).InitRoutes())

func (dr *DestRouter) InitRoutes() http.Handler {
    dr.router.Post("/{number}/product?version={v}", handlers.HandleProduct(dr.dest))
    return dr.router
}

But I try to hit this endpoint via post man I get a 404 not found

http://localhost:8345/api/dest/1235abc/product?version=1

May I know the issue here?

Zoyd
  • 3,449
  • 1
  • 18
  • 27
DoIt
  • 3,270
  • 9
  • 51
  • 103
  • 15
    Just a guess as I am not a `chi` user, but I assume it doesn't match against query parameters, so leave it out when registering the handler and use `r.URL.Query().Get("version")` to retrieve the value. – mkopriva Aug 20 '18 at 15:32

2 Answers2

21

As @mkopriva mentioned, simply use r.URL.Query().Get("version") to get the query parameter.

User1
  • 39,458
  • 69
  • 187
  • 265
0

Met this issue, found status 404, for your situation, only go with /{number}/product, do not need to add the content ?version={v}, chi only match with the path, and then use r.URL.Query().Get("version") to get the query parameters, it will work.

Lucas S.
  • 2,303
  • 1
  • 14
  • 20
Rick
  • 1