Get a request parameter key-value in fasthttp
Solution 1
Found it
ctx.QueryArgs().Peek("haha")
The naming choice is unexpected.
Solution 2
use Peek and PeekMulti
?haha=1
ctx.QueryArgs().Peek("haha")
?haha=1&haha=2
ctx.QueryArgs().PeekMulti("haha")
Some useful methods are declared here: https://github.com/valyala/fasthttp/blob/a1cfe58ca86648c6701f1cb7e8b1587348dd5b9f/args.go#L245
Solution 3
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
Related videos on Youtube
est
I speak Python and develop Django projects. I also hack a wide range of platforms.
Updated on June 04, 2022Comments
-
est almost 2 years
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?-
Sridhar over 7 yearsBy golang's fasthttp you mean this ?
-
est over 7 years@Sridhar sure yes.
-
Martin Gallagher over 7 years
haha := ctx.QueryArgs().GetUintOrZero("haha")
- godoc.org/github.com/valyala/fasthttp#RequestCtx.QueryArgs -
est over 7 years@MartinGallagher thanks. found it! It's the
Peek()
method.
-