> For the complete documentation index, see [llms.txt](https://denglj.gitbook.io/essential-go/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://denglj.gitbook.io/essential-go/common-standard-libraries/36-http-client/http-post.md).

# HTTP POST

HTTP POST发送二进制数据到服务端。

服务端应该如何解释这些数据取决于请求头中`Content-Type`的值。

## POST Url-Encoded 数据

HTTP POST通常用于向服务端提交HTML网页中填写的表单数据。

表单数据是键值对的字典，其中键是字符串，值是字符串数组（通常只含单个元素）。

表单键值对通常以URL编码的数据格式发送，`Content-Type`需设置为`application/x-www-form-urlencoded`。

```go
package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
	"time"
)

func main() {
	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)
}
```
