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
+5k Javascript : Change page title to get viewer attention
+12.8k Golang : List objects in AWS S3 bucket
+5.6k Cash Flow : 50 days to pay your credit card debt
+4.3k Java : Generate multiplication table example
+10k Golang : Convert file content to Hex
+24.9k Golang : Storing cookies in http.CookieJar example
+9.4k Javascript : Read/parse JSON data from HTTP response
+5.6k CodeIgniter/PHP : Remove empty lines above RSS or ATOM xml tag
+9.2k Golang : Convert(cast) string to int64
+4.8k Golang : Check if a word is countable or not
+44.4k Golang : Use wildcard patterns with filepath.Glob() example
+10.2k Swift : Convert (cast) String to Integer