How to preserve key case in request header using golang?

I recently used the golang library "net / http" adding some header information for the request, I found the title keys change like

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 receive a batch of http messages, I find that the header has changed as follows:

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

      

I am using golang 1.3, how do I keep the key case or keep its start? THX.

+3


source to share


1 answer


http.Header Add and Set canonicalize the header name when adding values ​​to the header map. You can sneak around 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 the headers known to the transport, this should work.

+8


source







All Articles