Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to keep key case sensitive in request header using golang?

I am recently using golang library "net/http",while add some header info to request, I found that the header keys are changing, e.g

request, _ := &http.NewRequest("GET", fakeurl, nil)
request.Header.Add("MyKey", "MyValue")
request.Header.Add("MYKEY2", "MyNewValue")
request.Header.Add("DONT-CHANGE-ME","No")

however, when I fetch the http message package, I found the header key changed like this:

Mykey: MyValue
Mykey2: MyNewValue
Dont-Change-Me:  No

I using golang 1.3, then how to keep key case sensitive or keep its origin looking? thx.

like image 971
crafet Avatar asked Oct 14 '14 02:10

crafet


People also ask

Is request headers case-sensitive?

An HTTP header consists of its case-insensitive name followed by a colon ( : ), then by its value.

Are CORS headers case-sensitive?

CORS Origin is Case Sensitive.

Is cache control case-sensitive?

Caching directives are case-insensitive. However, lowercase is recommended because some implementations do not recognize uppercase directives.

Is Curl case-sensitive?

It was always just case insensitive but some clients would preserve or enforce a special casing. curl for example typically passes on the casing used by the user - unless for HTTP/2 which is lowercase by spec.


1 Answers

The http.Header Add and Set methods canonicalize the header name when adding values to the header map. You can sneak around the canonicalization by adding values using map operations:

request.Header["MyKey"] = []string{"MyValue"}
request.Header["MYKEY2"] = []string{"MyNewValue"}
request.Header["DONT-CHANGE-ME"] = []string{"No"}

As long as you use canonical names for headers known to the transport, this should work.

like image 108
Bayta Darell Avatar answered Sep 29 '22 02:09

Bayta Darell