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
+10k Golang : Create S3 bucket with official aws-sdk-go package
+5.9k Mac OSX : Find large files by size
+4.7k Unix/Linux/MacOSx : Get local IP address
+7.1k Findstr command the Grep equivalent for Windows
+17.7k Golang : Populate dropdown with html/template example
+5.1k Golang : Extract unicode string from another unicode string example
+13.2k Golang : How to get URL port?
+9.2k Golang : How to tokenize source code with text/scanner package?
+21.3k Generate checksum for a file in Go
+18.1k Mac OSX : Homebrew and Golang
+10.9k Golang : Convert a rune to unicode style string \u