Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Golang Example for adding custom headers to a request.
package main
import (
func main() {
client := &http.Client{}
postData := make([]byte, 100)
req, err := http.NewRequest("POST", "", bytes.NewReader(postData))
if err != nil {
req.Header.Add("User-Agent", "myClient")
resp, err := client.Do(req)
defer resp.Body.Close()
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