Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Golang Example for adding custom headers to a request.
package main
import (
"bytes"
"fmt"
"net/http"
"os"
)
func main() {
client := &http.Client{}
postData := make([]byte, 100)
req, err := http.NewRequest("POST", "http://example.com", bytes.NewReader(postData))
if err != nil {
os.Exit(1)
}
req.Header.Add("User-Agent", "myClient")
resp, err := client.Do(req)
defer resp.Body.Close()
fmt.Println(resp)
}
@sekrett
Copy link

sekrett commented Nov 28, 2017

There is a nicer way for post data:

params := url.Values{}
params.Set("login", "a")
params.Set("password", "b")
postData := strings.NewReader(params.Encode())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment