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
+13.7k Golang : Execute function at intervals or after some delay
+8k Golang : Gorilla web tool kit schema example
+28.5k Golang : Get time.Duration in year, month, week or day
+8.6k Golang : Accessing content anonymously with Tor
+20.9k Golang : Convert seconds to minutes and remainder seconds
+6.3k Golang : Validate credit card example
+17.7k Golang : Padding data for encryption and un-padding data for decryption
+6.8k Golang : Shuffle strings array
+6.5k Golang : Dealing with postal or zip code example
+9.6k RPM : error: db3 error(-30974) from dbenv->failchk: DB_RUNRECOVERY: Fatal error, run database recovery