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
+15.6k Golang : Get file permission
+7.3k Gogland : Where to put source code files in package directory for rookie
+8.6k Golang : Get curl -I or head data from URL example
+13.3k Golang : unknown escape sequence error
+6.8k Golang : Check if one string(rune) is permutation of another string(rune)
+5.5k Golang : Markov chains to predict probability of next state example
+12.7k Golang : List objects in AWS S3 bucket
+18.8k Golang : Delete item from slice based on index/key position
+4.4k PHP : Extract part of a string starting from the middle
+20.7k Curl usage examples with Golang
+6.3k Golang : Check if password length meet the requirement
+20.8k Golang : How to read float value from standard input ?