Skip to content

Instantly share code, notes, and snippets.

@R4wm
Forked from kendellfab/golang-request
Last active December 12, 2020 04:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save R4wm/405eb358e9383dc8457a55025d0a8d68 to your computer and use it in GitHub Desktop.
Save R4wm/405eb358e9383dc8457a55025d0a8d68 to your computer and use it in GitHub Desktop.
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