Closed ManojKumarChauhan closed 3 years ago
Thanks for opening your first issue here! π Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord
https://docs.gofiber.io/api/ctx#response https://docs.gofiber.io/api/ctx#request
short methods for request header access https://docs.gofiber.io/api/ctx#get short methods for response header access https://docs.gofiber.io/api/ctx#getrespheader
As per https://docs.gofiber.io/api/ctx#getrespheader, if we know the header name then we can pick its value. I don't have header name and need some method which returns the name for all headers in response. Similarly, I need a function which returns all the cookies from request/response header.
app.Get("/test", func(ctx *fiber.Ctx) error {
// request
ctx.Request().Header.VisitAll(func(key, value []byte) {
fmt.Println("req headerKey", string(key), "value", string(value))
})
ctx.Request().Header.VisitAllCookie(func(key, value []byte) {
fmt.Println("req cookieKey", string(key), "value", string(value))
})
// response
ctx.Response().Header.VisitAll(func(key, value []byte) {
fmt.Println("res headerKey", string(key), "value", string(value))
})
ctx.Response().Header.VisitAllCookie(func(key, value []byte) {
fmt.Println("res cookieKey", string(key), "value", string(value))
})
return ctx.SendStatus(fiber.StatusOK)
})
Hi @ReneWerner87 , using above code for response header, i am able to get "Content-Type" only. Not able to get Content-Length. What about the case if response header also contains other keys like content-encoding etc? Will above function return other response headers also?
the header for the length of the content is set by fasthttp at the end, automatically
could show you how to set it manually, but i think we don't need that
yes all other headers you get like this
Thanks
Not sure who this might help, but a simpler method to just get one header is
ctx.Fasthttp.Request.Header.Peek("header-name")
token := c.GetReqHeaders()["Token"]
it don't support UTF-8 chars
eg:
curl -X 'POST' \
'http://127.0.0.1:8080/api/test/token' \
-H 'accept: application/json' \
-H 'Token: testUtf-8δΈζ' \
-d ''
output:
Undocumented | TypeError: Failed to execute 'fetch' on 'Window': Failed to read the 'headers' property from 'RequestInit': String contains non ISO-8859-1 code point.
Question description How to access headers and cookies from fiber's request and response. Code snippet Optional
I have following function where fiber context is passed func handle(c *fiber.Ctx) { }
I can access request headers using following code req := c.Request() headerString := req.Header.GetHeaders()
But I didn't find the code to access response header.
Also I didn't find any code to print all the cookies from fiber's request/response.
Can someone suggest?