gofiber / fiber

⚑️ Express inspired web framework written in Go
https://gofiber.io
MIT License
33.93k stars 1.67k forks source link

How to access headers and cookies πŸ€— #1591

Closed ManojKumarChauhan closed 3 years ago

ManojKumarChauhan commented 3 years ago

Question description How to access headers and cookies from fiber's request and response. Code snippet Optional

package main

import "github.com/gofiber/fiber/v2"

func main() {
  app := fiber.New()
  // ..
}

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?

welcome[bot] commented 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

ReneWerner87 commented 3 years ago

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

cookie https://docs.gofiber.io/api/ctx#cookies

ManojKumarChauhan commented 3 years ago

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.

ReneWerner87 commented 3 years ago
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)
    })
ManojKumarChauhan commented 3 years ago

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?

ReneWerner87 commented 3 years ago

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

ManojKumarChauhan commented 3 years ago

Thanks

chalukyaj commented 8 months ago

Not sure who this might help, but a simpler method to just get one header is ctx.Fasthttp.Request.Header.Peek("header-name")

yangqingrong commented 4 months ago

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.