Get a request parameter key-value in fasthttp

喜欢而已 提交于 2020-01-25 06:10:18

问题


http://127.0.0.1:8080/x?haha=1

I want to get something like ctx.QueryArgs().Get("haha")

is it possible in golang's fasthttp package?


回答1:


Found it

ctx.QueryArgs().Peek("haha")

The naming choice is unexpected.




回答2:


You can retrieve a custom GET, POST PUT parameter using FormValue method:
- GET (Query String such as ?user=a&pass=b);
- POST, PUT body

Literally, from the documentation:

FormValue returns form value associated with the given key.

The value is searched in the following places:

  • Query string;
  • POST or PUT body.

There are more fine-grained methods for obtaining form values:

  • QueryArgs for obtaining values from query string.
  • PostArgs for obtaining values from POST or PUT body.
  • MultipartForm for obtaining values from multipart form.
  • FormFile for obtaining uploaded files.
token = string(ctx.FormValue("token"))

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




回答3:



Another option while you don't have the ctx but have ctx.Request is this:

// somewhere
req := &ctx.Request
.
.
.
// somewhere else
req.URI().QueryArgs().Peek("somekey")


来源:https://stackoverflow.com/questions/39265978/get-a-request-parameter-key-value-in-fasthttp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!