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
+7.8k Golang : Test if an input is an Armstrong number example
+12.2k Golang : Setup API server or gateway with Caddy and http.ListenAndServe() function example
+17.2k Golang : Get input from keyboard
+4.9k Adding Skype actions such as call and chat into web page examples
+9.2k Golang : What is the default port number for connecting to MySQL/MariaDB database ?
+16.8k Golang : Delete files by extension
+14k Golang : Gin framework accept query string by post request example
+27.6k Golang : Convert CSV data to JSON format and save to file
+6.8k Golang : Skip or discard items of non-interest when iterating example
+10.8k Golang : Bubble sort example
+11.9k Android Studio : Create custom icons for your application example
+15.9k Golang : Get checkbox or extract multipart form data value example