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.6k Fix sudo yum hang problem with no output or error messages
+22.7k Golang : Gorilla mux routing example
+12.6k Golang : Convert int(year) to time.Time type
+7k Golang : Use modern ciphers only in secure connection
+5k Golang : Print instead of building pyramids
+8.9k Golang : Serving HTTP and Websocket from different ports in a program example
+13.1k Golang : error parsing regexp: invalid or unsupported Perl syntax
+9.3k Golang : Get all countries currencies code in JSON format
+5.2k Golang : Return multiple values from function
+9.8k Golang : Setting variable value with ldflags
+7.1k Golang : Fixing Gorilla mux http.FileServer() 404 problem
+8.8k Golang : io.Reader causing panic: runtime error: invalid memory address or nil pointer dereference