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
+37.5k Upload multiple files with Go
+19.6k Golang : Close channel after ticker stopped example
+6.5k Golang : Handling image beyond OpenCV video capture boundary
+13.9k Golang : convert(cast) string to float value
+17.7k Golang : Parse date string and convert to dd-mm-yyyy format
+18.1k Golang : Convert IPv4 address to decimal number(base 10) or integer
+6.6k Golang : Embedded or data bundling example
+5.5k How to check with curl if my website or the asset is gzipped ?
+11.8k Golang : convert(cast) float to string
+9.3k Golang : Create and shuffle deck of cards example
+40.6k Golang : Convert to io.ReadSeeker type
+34.7k Golang : How to stream file to client(browser) or write to http.ResponseWriter?