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
+24.6k Golang : missing Mercurial command
+16.3k How to enable MariaDB/MySQL logs ?
+3.9k HTTP common errors and their meaning explained
+4.1k Golang : Get FX sentiment from website example
+17.8k Golang : How to get own program name during runtime ?
+12.6k Golang : Get current time
+30.9k Golang : Smarter Error Handling with strings.Contains()
+5.1k Javascript : Generate random key with specific length
+19.7k Golang : How to get time zone and load different time zone?
+4.6k Swift : Get substring with rangeOfString() function example
+16.3k Golang : Upload/Receive file progress indicator
+27.6k Golang : Get first few and last few characters from string