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.8k Golang : Command line ticker to show work in progress
+3.3k Golang : Calculate a pip value and distance to target profit example
+16.9k Golang : Check if directory exist and create if does not exist
+7.4k Golang : does not implement flag.Value (missing Set method)
+8k Golang : Sort and reverse sort a slice of floats
+5.3k Golang : A simple forex opportunities scanner
+10.2k Golang : 2 dimensional array example
+10.5k Golang : ROT47 (Caesar cipher by 47 characters) example
+8.2k Golang : Channels and buffered channels examples
+11.3k Golang : Date and Time formatting
+13.6k Golang : How to check if input from os.Args is integer?
+10.5k Golang : Extract part of string with regular expression