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
+12.6k Golang : Qt image viewer example
+13.2k Golang : Get file permission
+3.7k Golang : Struct field tags and what is their purpose?
+6k PHP : How to parse ElasticSearch JSON ?
+6.3k Golang : Take screen shot of browser with JQuery example
+15.2k Golang : GORM create record or insert new record into database example
+16.7k Golang : Delete item from slice based on index/key position
+9k How to test Facebook App on localhost ?
+12k Golang : How to tell if a file is compressed either gzip or zip ?
+13.3k Golang : Fix cannot download, $GOPATH not set error
+4k Golang : How to get capacity of a slice or array?
+6.2k Golang : How To Use Panic and Recover