Essential Go HTTP Client  Suggest an edit

HTTP POST

HTTP POST sends binary data to the server.

How this data is interpreted by the server depends on the value of Content-Type header.

HTTP POST with url-encoded data

HTTP POST is most often used to submit filled forms data in HTML page to the server.

Form data is a dictionary of key/value pairs where key is a string and value is array of strings (most often array with a single element).

Form key/value pairs are most often sent as url-encoded data with Content-Type of application/x-www-form-urlencoded.

client := &http.Client{}
client.Timeout = time.Second * 15

uri := "https://httpbin.org/post"
data := url.Values{
	"name":  []string{"John"},
	"email": []string{"john@gmail.com"},
}
resp, err := client.PostForm(uri, data)
if err != nil {
	log.Fatalf("client.PosFormt() failed with '%s'\n", err)
}
defer resp.Body.Close()
_, err = ioutil.ReadAll(resp.Body)
if err != nil {
	log.Fatalf("ioutil.ReadAll() failed with '%s'\n", err)
}

fmt.Printf("PostForm() sent '%s'. Response status code: %d\n", data.Encode(), resp.StatusCode)
PostForm() sent 'email=john%40gmail.com&name=John'. Response status code: 200

  ↑ ↓ to navigate     ↵ to select     Esc to close