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
+22.6k Golang : Daemonizing a simple web server process example
+8.8k Golang : Setting variable value with ldflags
+3.8k Python : Find out the variable type and determine the type with simple test
+14.8k Golang : Get IP addresses of a domain name
+7.2k Python : Fix SyntaxError: Non-ASCII character in file, but no encoding declared
+11.3k Elastic Search : Return all records (higher than default 10)
+5.6k Golang : Find the shortest line of text example
+22k Golang : Fix type interface{} has no field or no methods and type assertions example
+11.8k Golang : Handle or parse date string with Z suffix(RFC3339) example
+7.9k Golang : Generate random Chinese, Japanese, Korean and other runes
+8.4k Golang : Quadratic example