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
+5.5k Golang : Configure crontab to poll every two minutes 8am to 6pm Monday to Friday
+8.9k Golang : What is the default port number for connecting to MySQL/MariaDB database ?
+5.3k Golang : Reclaim memory occupied by make() example
+18.6k Golang : Find IP address from string
+8.9k Golang : Build and compile multiple source files
+27.4k Golang : dial tcp: too many colons in address
+19.2k Golang : Get RGBA values of each image pixel
+8.7k Android Studio : Image button and button example
+11.4k Golang : Generate DSA private, public key and PEM files example
+16.6k Golang : Gzip file example
+5.4k Gogland : Datasource explorer
+6.7k Golang : Get expvar(export variables) to work with multiplexer