DeepSource has joined Harness. Learn more
Go logoGo/
SCC-SA1008

Non-canonical key in `http.Header` mapSCC-SA1008

Major severityMajor
Bug Risk categoryBug Risk

Keys in http.Header maps are canonical, meaning they follow a specific combination of uppercase and lowercase letters. Methods such as http.Header.Add and http.Header.Del convert inputs into this canonical form before manipulating the map.

Bad practice

When manipulating http.Header maps directly, instead of using the provided methods, care should be taken to stick to canonical form to avoid inconsistencies. The following piece of code demonstrates one such inconsistency:

h := http.Header{}
h["etag"] = []string{"1234"}
h.Add("etag", "5678")
fmt.Println(h) // map[Etag:[5678] etag:[1234]]

The easiest way of obtaining the canonical form of a key is to use http.CanonicalHeaderKey:

h := http.Header{}
h[http.CanonicalHeaderKey("etag")] = []string{"1234"}
h.Add("etag", "5678")
fmt.Println(h) // map[Etag:[1234 5678]]