Get/Set HTTP Headers in Go Request
HTTP headers, we all need ’em 😉
Here’s how you can get and set these headers within your Go requests. These include request coming into your router handlers and requests you are sending out to other systems through
net/http. This can be thought of as being the same as reading headers from a request and creating new ones.
First we’ll start with reading them from the request.
The headers are accessible through the
Header part of the request - and from here you can get a specific header, through calling
If the header isn’t set, or is empty, it will return an empty string.
You can call
.Set() in a similar way to the reading the headers, but you will also need to pass in the value to set it to. In our example above we set the content type of the request to json.