How to set HTTP headers

Set HTTP headers in Go by using the Header.Set method on Request or ResponseWriter objects, or control HTTP behavior via GODEBUG environment variables.

Set HTTP headers by assigning key-value pairs to the Header map on your http.Request or http.ResponseWriter object.

req.Header.Set("Content-Type", "application/json")
resp.Header.Set("X-Custom-Header", "value")

For environment-level HTTP behavior control, use the GODEBUG variable with settings like http2client=0 to disable HTTP/2.