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.1k Unix/Linux : Use netstat to find out IP addresses served by your website server
+6.3k Golang : Convert an executable file into []byte example
+15.6k Golang : Read a file line by line
+14.2k Golang : Send email with attachment(RFC2822) using Gmail API example
+7.9k Golang : How To Use Panic and Recover
+23.6k Golang : Use regular expression to validate domain name
+14k Golang : Convert IP version 6 address to integer or decimal number
+7.8k Golang : Sort words with first uppercase letter
+11.1k Android Studio : Create custom icons for your application example
+4.7k Golang : Calculate a pip value and distance to target profit example
+14.9k Golang : Accurate and reliable decimal calculations
+9.1k Golang : How to get ECDSA curve and parameters data?