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
+13.4k Golang : Get user input until a command or receive a word to stop
+22.5k Golang : Strings to lowercase and uppercase example
+5.9k Fix ERROR 2003 (HY000): Can't connect to MySQL server on 'IP address' (111)
+36.2k Golang : Convert date or time stamp from string to time.Time type
+11.1k Golang : How to pipe input data to executing child process?
+7k Golang : Array mapping with Interface
+5.2k Unix/Linux/MacOSx : How to remove an environment variable ?
+7.7k Golang : How to feed or take banana with Gorilla Web Toolkit Session package
+5.3k Gogland : Datasource explorer
+4.6k JavaScript: Add marker function on Google Map
+21.2k Curl usage examples with Golang