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
+10k How to test Facebook App on localhost ?
+7.1k Golang : HTTP Server Example
+13.4k Golang : package is not in GOROOT during compilation
+6.6k Gogland : Single File versus Go Application Run Configurations
+6.5k Golang : Detect sample rate, channels or latency with PortAudio
+13.3k Golang : Parsing or breaking down URL
+10.1k Golang : Find age or leap age from date of birth example
+10k Golang : Characters limiter example
+5.4k Golang : Convert an executable file into []byte example
+8.4k Golang : Quadratic example
+27.5k Golang : Saving(serializing) and reading file with GOB
+6.2k Golang : Of hash table and hash map