websocket icon indicating copy to clipboard operation
websocket copied to clipboard

How to get Request Headers in websocket context

Open surjit opened this issue 2 years ago • 1 comments

surjit avatar Mar 09 '22 16:03 surjit

Hi @surjit am not sure why you wanna put the headers in the context but you can use c.locals(key,value) to send the headers from the http request using a middleware before the upgrade.

here's an example

func SocketMiddleware(c *fiber.Ctx) error {
	if websocket.IsWebSocketUpgrade(c) {
		token := c.GetReqHeaders()["Authorization"]
		c.Locals("token", token)
		return c.Next()
	}
	return fiber.ErrUpgradeRequired
}

oSethoum avatar Mar 02 '23 11:03 oSethoum