1

I am using golang´s fasthttprouter and have followed the examples and defined a router like this:

router.GET("/customer/account/detail/:accountId", myHandler.customerAccountDetailHandler)

Then I call to my service as http://locahost:9296/customer/account/detail/2

But I realised that I do not want to have the parameters as part of the endpoint , I rather prefer to use normal parameters by calling my service like this:

http://locahost:9296/customer/account/detail?accountId=2&sort=1

Is it possible to be done with fasthttprouter? How?

Thanks in advance J

antonof
  • 109
  • 1
  • 8

2 Answers2

0

The query parameter should be accessible from the request context. You should have a handler that takes a *fasthttp.RequestCtx argument. This RequestCtx can access the URI and the query params on that URI. That should look something like this:

ctx.URI().QueryArgs().Peek("accountId")

You'll have to update your handler to use this query parameter instead of the route param you were previously using. The same would also apply for the sort param.

Also, your router would have to be updated to route /customer/account/detail to your updated handler (i.e. you'll want to remove /:accountId from your route).

benjaminjosephw
  • 4,369
  • 3
  • 21
  • 40
  • So, basically I should stop using the fasthttprouter package and use the default fasthttp handlers. Am I right? – antonof Mar 19 '19 at 13:35
  • @antonof I don't think you need to change any packages to get this working. You only need to change the behaviour of `myHandler.customerAccountDetailHandler` to extract the `accountId` from the query param using the code snippet above and update the router to `router.GET("/customer/account/detail", myHandler.customerAccountDetailHandler)`. – benjaminjosephw Mar 19 '19 at 13:39
0

Your questions is similar to this one:
Get a request parameter key-value in fasthttp

You can retrieve the parameters of the request in this way:

token = string(ctx.FormValue("token"))

Have a look at my complete response here

https://stackoverflow.com/a/57740178/9361998

Documentation: https://godoc.org/github.com/valyala/fasthttp#RequestCtx.FormValue

alessiosavi
  • 2,753
  • 2
  • 19
  • 38