Golang : Set or Add HTTP Request Headers
Problem :
While coding the tutorial on how to interface with PayPal, I need to add header value for Content-Type before posting to PayPal. How to Set or Add HTTP Request Headers?
Solution :
Header (http://golang.org/pkg/net/http/#Header) has Add() and Set() methods.
Example 1:
req, err := http.NewRequest("GET", "http://example.com", nil)
req.Header.Set("name", "value")
Example 2:
// post data back to PayPal
client := &http.Client{}
req, err := http.NewRequest("POST", postStr, nil)
if err != nil {
fmt.Println(err)
return
}
req.Header.Add("Content-Type: ", "application/x-www-form-urlencoded")
resp, err := client.Do(req)
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+12.9k Golang : Convert IP version 6 address to integer or decimal number
+6.6k Golang : What fmt.Println() can do and println() cannot do
+15.7k Golang : How to tell if a file is compressed either gzip or zip ?
+10.5k Golang : Sort and reverse sort a slice of runes
+39.7k Golang : How to check if a string contains another sub-string?
+5.4k Grep : How to grep for strings inside binary data
+17.3k Golang : Example for RSA package functions
+5.6k Golang : How to setup a disk space used monitoring service with Telegram bot
+7.6k Golang : Executing and evaluating nested loop in html template
+25.1k Golang : Encrypt and decrypt data with AES crypto
+5.8k Fix sudo yum hang problem with no output or error messages
+9.6k Golang : Get UDP client IP address and differentiate clients by port number