Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain all request headers in Go

Tags:

go

How can I obtain all available http headers from a request as array in Go? I see only the following two methods:

  • Header(name string, value string)
  • GetHeader(name string)

But in this case I need to know the name of the Header and can't return all existing headers. I'd like to copy the http headers from one request to anther one.

like image 988
botscripter Avatar asked Nov 29 '17 16:11

botscripter


1 Answers

Use Request.Header to access all headers. Because Header is a map[string][]string, two loops are required to access all headers.

// Loop over header names
for name, values := range r.Header {
    // Loop over all values for the name.
    for _, value := range values {
        fmt.Println(name, value)
    }
}
like image 166
Bayta Darell Avatar answered Sep 21 '22 01:09

Bayta Darell