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
+7k Golang : Takes a plural word and makes it singular
+12.2k Golang : Print UTF-8 fonts on image example
+13.4k Golang : Read XML elements data with xml.CharData example
+10k Golang : Setting variable value with ldflags
+15.3k Golang : Find location by IP address and display with Google Map
+51.3k Golang : Check if item is in slice/array
+11k Golang : Simple image viewer with Go-GTK
+6k Golang : How to verify input is rune?
+27.1k Golang : Find files by name - cross platform example
+9.7k Golang : Load ASN1 encoded DSA public key PEM file example
+20.1k Golang : Determine if directory is empty with os.File.Readdir() function
+16.7k Golang : Read integer from file into array