How to Set Headers in HTTP Request in Go
In Go, you set headers on an HTTP request by using the
Header.Set method on the http.Request object. First, create a request with http.NewRequest, then add headers like req.Header.Set("Key", "Value") before sending the request with an http.Client.Syntax
To set headers in an HTTP request in Go, you first create a request object, then use the Header.Set method to add or update headers.
http.NewRequest(method, url, body): creates a new HTTP request.req.Header.Set(key, value): sets a header key to a value.client.Do(req): sends the request.
go
req, err := http.NewRequest("GET", "https://example.com", nil) if err != nil { // handle error } req.Header.Set("Content-Type", "application/json") req.Header.Set("Authorization", "Bearer token") client := &http.Client{} resp, err := client.Do(req) if err != nil { // handle error } defer resp.Body.Close()
Example
This example shows how to create a GET request, set two headers, send the request, and print the response status.
go
package main import ( "fmt" "net/http" ) func main() { req, err := http.NewRequest("GET", "https://httpbin.org/get", nil) if err != nil { fmt.Println("Error creating request:", err) return } // Set headers req.Header.Set("User-Agent", "MyGoClient/1.0") req.Header.Set("Accept", "application/json") client := &http.Client{} resp, err := client.Do(req) if err != nil { fmt.Println("Error sending request:", err) return } defer resp.Body.Close() fmt.Println("Response status:", resp.Status) }
Output
Response status: 200 OK
Common Pitfalls
Common mistakes when setting headers in Go include:
- Trying to set headers on a
nilrequest object before creating it. - Using
req.Header.Addrepeatedly for the same key when you want to replace the value (useSetinstead). - Not setting headers before sending the request.
- Confusing request headers with response headers.
go
package main import ( "fmt" "net/http" ) func main() { // Wrong: setting headers before creating request var req *http.Request // This will panic because req is nil // req.Header.Set("Authorization", "token") // Correct way: req, err := http.NewRequest("GET", "https://example.com", nil) if err != nil { fmt.Println("Error:", err) return } req.Header.Set("Authorization", "token") // Wrong: using Add instead of Set when replacing header req.Header.Add("Content-Type", "application/json") req.Header.Add("Content-Type", "text/plain") // adds another header instead of replacing // Right: use Set to replace req.Header.Set("Content-Type", "text/plain") }
Quick Reference
| Action | Method | Description |
|---|---|---|
| Create request | http.NewRequest(method, url, body) | Creates a new HTTP request object |
| Set header | req.Header.Set(key, value) | Sets or replaces a header key with a value |
| Add header | req.Header.Add(key, value) | Adds a header value without replacing existing ones |
| Send request | client.Do(req) | Sends the HTTP request and returns a response |
Key Takeaways
Always create the request with http.NewRequest before setting headers.
Use req.Header.Set to add or replace a header value.
Set all headers before sending the request with client.Do.
Avoid using Header.Add when you want to replace a header value.
Check for errors when creating requests and sending them.