$Id: 200
$SOId: 4643
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 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
.
// no playground
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"time"
)
func main() {
// :show start
client := &http.Client{}
client.Timeout = time.Second * 15
uri := "<https://httpbin.org/post>"
data := url.Values{
"name": []string{"John"},
"email": []string{"[email protected]"},
}
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)
// :show end
}