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
+21.3k Golang : Convert string slice to struct and access with reflect example
+12.5k Golang : http.Get example
+7.6k Javascript : How to check a browser's Do Not Track status?
+8.1k Golang : Generate Datamatrix barcode
+6.4k Golang : Warp text string by number of characters or runes example
+15.1k Golang : invalid character ',' looking for beginning of value
+13.1k Golang : Get constant name from value
+5.7k Golang : Use NLP to get sentences for each paragraph example
+9.4k Golang : Copy map(hash table) example
+11.4k How to tell if a binary(executable) file or web application is built with Golang?
+5.5k Linux/Unix/PHP : Restart PHP-FPM
+9.8k Golang : Check a web page existence with HEAD request example