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
+29.7k Golang : Saving(serializing) and reading file with GOB
+25.4k Golang : Convert uint value to string type
+17.3k Golang : Capture stdout of a child process and act according to the result
+6.6k Golang : Handling image beyond OpenCV video capture boundary
+9.8k PHP : Get coordinates latitude/longitude from string
+8.6k Golang : How to check variable or object type during runtime?
+6.2k Golang : Function as an argument type example
+5.7k Unix/Linux : How to find out the hard disk size?
+21.5k Golang : Create and resolve(read) symbolic links
+36.2k Golang : Get file last modified date and time
+15.8k Golang : Get checkbox or extract multipart form data value example
+4.8k JavaScript: Add marker function on Google Map