1

我正在使用 golang 的 fasthttprouter 并遵循示例并定义了这样的路由器:

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

然后我打电话给我的服务http://locahost:9296/customer/account/detail/2

但是我意识到我不想将参数作为端点的一部分,我宁愿通过像这样调用我的服务来使用普通参数:

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

是否可以使用 fasthttprouter 完成?如何?

提前谢谢

4

2 回答 2

0

您的问题与此类似:
Get a request parameter key-value in fasthttp

您可以通过这种方式检索请求的参数:

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

在这里查看我的完整回复

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

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

于 2019-09-14T12:54:04.600 回答
0

查询参数应该可以从请求上下文中访问。您应该有一个接受*fasthttp.RequestCtx参数的处理程序。这RequestCtx可以访问 URI 和该 URI 上的查询参数。这应该看起来像这样:

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

您必须更新您的处理程序以使用此查询参数而不是您之前使用的路由参数。这同样适用于sort参数。

此外,您的路由器必须更新以路由/customer/account/detail到您更新的处理程序(即您希望/:accountId从您的路由中删除)。

于 2019-03-19T11:22:45.960 回答