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
+9.4k Golang : Launch Mac OS X Preview (or other OS) application from your program example
+19.7k Golang : Close channel after ticker stopped example
+7.9k Golang : Load DSA public key from file example
+14.7k Golang : GUI with Qt and OpenCV to capture image from camera
+7k Fix sudo yum hang problem with no output or error messages
+8.2k Golang : Tell color name with OpenCV example
+5.8k Unix/Linux/MacOSx : Get local IP address
+17k Golang : Set up source IP address before making HTTP request
+27k Golang : Force your program to run with root permissions
+14k Golang : Get current time
+17.3k Golang : Find file size(disk usage) with filepath.Walk
+8k Golang Hello World Example