简体   繁体   中英

Define parameters as param request instad of endpoint url in fasthttprouter

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

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 (ie you'll want to remove /:accountId from your route).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM