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
+24.9k Golang : Create PDF file from HTML file
+7.3k Golang : Create zip/ePub file without compression(use Store algorithm)
+9.6k Golang : Find correlation coefficient example
+7.3k Golang : Scanf function weird error in Windows
+15.6k Golang : Get digits from integer before and after given position example
+15.9k Golang : Generate universally unique identifier(UUID) example
+13k Golang : Convert(cast) int to int64
+9.6k PHP : Get coordinates latitude/longitude from string
+8.9k Golang : Handle sub domain with Gin
+16.3k Golang : Send email and SMTP configuration example
+10.4k Golang : Select region of interest with mouse click and crop from image
+37.9k Golang : Read a text file and replace certain words