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
+6.8k Golang : Muxing with Martini example
+12.2k Golang : Flush and close file created by os.Create and bufio.NewWriter example
+23.3k Golang : Get ASCII code from a key press(cross-platform) example
+20k Golang : Reset or rewind io.Reader or io.Writer
+10.6k Golang : Flip coin example
+8k Golang : How To Use Panic and Recover
+32k Golang : Validate email address with regular expression
+11.5k SSL : The certificate is not trusted because no issuer chain was provided
+34k Golang : Create x509 certificate, private and public keys
+25.6k Golang : missing Mercurial command
+20.5k Golang : Secure(TLS) connection between server and client
+3.7k Java : Random alphabets, alpha-numeric or numbers only string generator