Mega
Mega

Reputation: 45

Cannot get response header with the same key name (*http.Header)

Let's say I have 2 response headers with the same name "Set-Cookie"

The .Get method can only get the first value

In the document it said: To access multiple values of a key, or to use non-canonical keys, access the map directly.

fmt.Println(headers.Get("Set-Cookie"))
// -> Can only get the first one

However when read the map

for key,value := range headers {
  fmt.Println(key, value)
}
// -> cannot range over headers (type *http.Header)

Currently I'm not sure the right way to read this data.

Upvotes: 0

Views: 436

Answers (2)

hayashi-ay
hayashi-ay

Reputation: 299

Use Values instead of Get. Values returns all values associated with the given key.

Upvotes: 2

Burak Serdar
Burak Serdar

Reputation: 51497

http.Header is already a map[string][]string so there should be no need to pass the address of it around. If you somehow passed &request.Header, then you should pass it directly instead, without taking the address. If you have to pass the address for some reason, range over *headers instead of headers:

for key,value := range *headers {
}

Upvotes: 2

Related Questions