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
+6k Golang : Fibonacci number generator examples
+16.7k Golang : Convert IPv4 address to decimal number(base 10) or integer
+8.2k Golang : Detect number of active displays and the display's resolution
+11.7k Golang : http.Get example
+10.6k Golang : Setup API server or gateway with Caddy and http.ListenAndServe() function example
+12k Golang : Linear algebra and matrix calculation example
+50.4k Golang : How to get struct field and value by name
+11.6k Golang : Drop cookie to visitor's browser and http.SetCookie() example
+10.4k Golang : Generate DSA private, public key and PEM files example
+8.8k Golang : Check a web page existence with HEAD request example
+11.1k Golang : Forwarding a local port to a remote server example
+12.3k Golang : Get constant name from value