如何使用Golang发送HTTP POST请求_Golang net HTTP Post方法示例

3次阅读

go语言发送http POST请求最常用net/http包的http.Post或http.Client.Do方法;前者适合简单表单,后者支持自定义头、超时等;上传文件需用multipart.Writer构造请求体;响应需关闭Body并检查状态码与错误。

如何使用Golang发送HTTP POST请求_Golang net HTTP Post方法示例

Go 语言中发送 HTTP POST 请求最常用的方式是使用 net/http 包的 http.Post 或更灵活的 http.Client.Do 方法。前者适合简单场景,后者更适合控制请求头、超时、认证等细节。

使用 http.Post 发送简单表单数据

适用于向服务器提交 application/x-www-form-urlencoded 类型的数据(如登录表单)。

  • 调用 http.Post(url, contentType, body),其中 body 需为 io.Reader 类型
  • 常用做法:用 strings.NewReader 包装 URL 编码后的字符串
  • 注意设置正确的 Content-Type 头(http.Post 不自动设,需手动或改用 http.NewRequest

示例:

如何使用Golang发送HTTP POST请求_Golang net HTTP Post方法示例

文心智能体平台

百度推出的基于文心大模型的Agent智能体平台,已上架2000+ai智能体

如何使用Golang发送HTTP POST请求_Golang net HTTP Post方法示例 393

查看详情 如何使用Golang发送HTTP POST请求_Golang net HTTP Post方法示例

resp, err := http.Post("https://www.php.cn/link/dc076eb055ef5f8a60a41b6195e9f329", "application/x-www-form-urlencoded",       strings.NewReader("name=alice&age=30"))   if err != nil {       log.Fatal(err)   }   defer resp.Body.Close()   body, _ := io.ReadAll(resp.Body)   fmt.Println(string(body))

使用 http.NewRequest + Client.Do 发送 jsON 数据

这是更推荐的方式,尤其在需要自定义请求头(如 AuthorizationAccept)、设置超时、复用连接时。

立即学习go语言免费学习笔记(深入)”;

  • 先用 json.Marshal 序列化结构体map
  • bytes.NewReader 将 JSON 字节切片转为 io.Reader
  • 通过 http.NewRequest 创建请求,再手动设置 Content-Type: application/json
  • 用自定义 http.Client(可设超时)调用 Do

示例:

data := map[string]string{"title": "Hello", "content": "World"}   jsonBytes, _ := json.Marshal(data)   <p>req, _ := http.NewRequest("POST", "<a href="https://www.php.cn/link/dc076eb055ef5f8a60a41b6195e9f329">https://www.php.cn/link/dc076eb055ef5f8a60a41b6195e9f329</a>", bytes.NewReader(jsonBytes))<br /> req.Header.Set("Content-Type", "application/json")<br /> req.Header.Set("User-Agent", "MyApp/1.0")  </p><p>client := &http.Client{Timeout: 10 * time.Second}<br /> resp, err := client.Do(req)<br /> if err != nil {<br /> log.Fatal(err)<br /> }<br /> defer resp.Body.Close()

上传文件或混合表单(multipart/form-data)

当需要同时传文本字段和文件时,必须构造 multipart 请求体。

  • multipart.Writer 创建请求体,它会自动生成边界(boundary)和对应头
  • 调用 w.WriteField 添加普通字段,w.CreateFormFile 添加文件字段
  • w 的输出作为请求 body,并设置 Content-Typew.FormDataContentType()

示例(仅传文本字段,模拟表单):

body := &bytes.Buffer{}   w := multipart.NewWriter(body)   w.WriteField("username", "bob")   w.WriteField("email", "bob@example.com")   w.Close()   <p>req, _ := http.NewRequest("POST", "<a href="https://www.php.cn/link/dc076eb055ef5f8a60a41b6195e9f329">https://www.php.cn/link/dc076eb055ef5f8a60a41b6195e9f329</a>", body)<br /> req.Header.Set("Content-Type", w.FormDataContentType())  </p><p>resp, _ := http.DefaultClient.Do(req)

处理响应与常见错误

无论哪种方式,响应处理逻辑一致,但容易忽略几个关键点:

  • 务必调用 resp.Body.Close(),否则可能造成连接泄漏
  • 检查 resp.StatusCode,2xx 不一定代表业务成功(比如 400 可能返回 JSON 错误信息)
  • io.ReadAll 读取完整响应体,避免只读前几字节导致 body 未关闭
  • 网络错误(如超时、dns 失败)发生在 Do 阶段;服务端返回错误状态码属于正常响应,需业务判断

基本上就这些。不复杂但容易忽略细节,尤其是超时控制和资源释放。

text=ZqhQzanResources