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
+26.9k Golang : Find files by extension
+11.3k Golang : Characters limiter example
+11k Golang : Create S3 bucket with official aws-sdk-go package
+6.9k How to let Facebook Login button redirect to a particular URL ?
+17.1k Golang : XML to JSON example
+6.4k PHP : Proper way to get UTF-8 character or string length
+9.2k Golang : How to find out similarity between two strings with Jaro-Winkler Distance?
+16.3k Golang : How to extract links from web page ?
+7.8k Swift : Convert (cast) String to Double
+15.9k Golang : Read a file line by line
+16.4k Golang :Trim white spaces from a string
+8.2k Golang : HttpRouter multiplexer routing example