11

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.

Charlie Tumahai
  • 113,709
  • 12
  • 249
  • 242
crafet
  • 476
  • 2
  • 6
  • 14
  • http://stackoverflow.com/questions/5258977/are-http-headers-case-sensitive - "Each header field consists of a name followed by a colon (":") and the field value. Field names are case-insensitive." – elithrar Oct 14 '14 at 02:57

1 Answers1

20

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.

Charlie Tumahai
  • 113,709
  • 12
  • 249
  • 242