How to get the header content from *fasthttp.request in Golang?

by roel_stanton , in category: Golang , a year ago

How to get the header content from *fasthttp.request in Golang?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by alfonzo , a year ago

@roel_stanton 

In order to get the header content from a fasthttp.Request in Golang, you can use the Header method on the request object. The Header method returns an instance of fasthttp.RequestHeader which contains the headers sent with the request.


Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package main

import (
	"fmt"
	"github.com/valyala/fasthttp"
)

func main() {
	req := fasthttp.AcquireRequest()
	defer fasthttp.ReleaseRequest(req)

	req.SetRequestURI("http://example.com")
	req.Header.Set("X-Custom-Header", "custom-value")

	header := req.Header

	// Print all headers
	header.VisitAll(func(key, value []byte) {
		fmt.Printf("%s: %s
", key, value)
	})

	// Get a specific header
	customHeaderValue := string(header.Peek("X-Custom-Header"))
	fmt.Println("Custom header value:", customHeaderValue)
}


In this example, we create a fasthttp.Request object and set a custom header with the Set method. Then, we get a reference to the request header using the Header method and iterate over all the headers using the VisitAll method. Finally, we get the value of a specific header using the Peek method.


Note that in the example, we use fasthttp.AcquireRequest() and fasthttp.ReleaseRequest(req) to acquire and release the request object. This is because fasthttp uses a custom memory pool to avoid allocations during the handling of requests, so it's important to release the request object back to the pool after use.

by hershel.jaskolski , 4 months ago

@roel_stanton 

Here's a modified example using fasthttp.RequestCtx to extract the header content from a fasthttp request:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package main

import (
	"fmt"
	"github.com/valyala/fasthttp"
)

func main() {
	req := fasthttp.AcquireRequest()
	defer fasthttp.ReleaseRequest(req)

	req.SetRequestURI("http://example.com")
	req.Header.Set("X-Custom-Header", "custom-value")

	ctx := fasthttp.RequestCtx{}
	ctx.Init(req, nil, nil)

	header := ctx.Request.Header

	// Print all headers
	header.VisitAll(func(key, value []byte) {
		fmt.Printf("%s: %s
", key, value)
	})

	// Get a specific header
	customHeaderValue := string(header.Peek("X-Custom-Header"))
	fmt.Println("Custom header value:", customHeaderValue)
}


In this modified example, we create a fasthttp.Request object and set a custom header with the Set method. We then initialize a fasthttp.RequestCtx object using the initialized fasthttp.Request object. The fasthttp.RequestCtx is useful when dealing with the request context, as it provides additional functionalities.


We then use the RequestCtx.Request.Header property to get a reference to the request header. We can iterate over all the headers using the VisitAll method, or retrieve the value of a specific header using the Peek method. Note that we convert the header value to a string to make it readable.