17370845950

如何使用Golang实现HTTP客户端请求_发送GET和POST请求示例
Go标准库net/http包提供并发安全的HTTP客户端,推荐用http.Client配合http.NewRequest发送GET/POST请求,需设置Header、超时及关闭resp.Body;支持JSON和表单提交,并可自定义重定向行为。

Go语言标准库中的net/http包提供了简洁、高效、并发安全的HTTP客户端支持,无需第三方依赖即可完成GET/POST等常见请求。关键在于正确构造http.Request、设置必要的Header(如Content-Type)、处理响应体并及时关闭Body。

发送GET请求(带查询参数)

使用http.Get最简单,但无法自定义Header或超时;更推荐用http.Client配合http.NewRequest,便于控制细节。

  • url.Values构建查询参数,再拼接到URL后
  • 创建http.Client可设置超时,避免请求永久挂起
  • 务必调用resp.Body.Close()释放连接,否则可能耗尽文件描述符
示例代码:
client := &http.Client{Timeout: 10 * time.Second}
params := url.Values{}
params.Set("q", "golang")
params.Set("page", "1")
urlStr := "https://api.example.com/search?" + params.Encode()

req, err := http.NewRequest("GET", urlStr, nil) if err != nil { log.Fatal(err) } req.Header.Set("User-Agent", "MyApp/1.0")

resp, err := client.Do(req) if err != nil { log.Fatal(err) } defer resp.Body.Close() // 关键:必须关闭

body, _ := io.ReadAll(resp.Body) fmt.Println(string(body))

发送JSON格式的POST请求

向API提交结构化数据时,通常用application/json类型。需将Go结构体序列化为JSON字节,并设好Header。

  • json.Marshal将struct转为[]byte,直接传入bytes.NewReader()作为Body
  • 必须显式设置Content-Type: application/json
  • 检查resp.StatusCode是否为2xx,避免只看err而忽略业务错误(如400/500)
示例代码:
type LoginReq struct {
    Username string `json:"username"`
    Password string `json:"password"`
}

data := LoginReq{Username: "user", Password: "pass"} jsonBytes, _ := json.Marshal(data)

req, _ := http.NewRequest("POST", "https://www./link/052c3ffc93bd3a4d5fc379bf96fabea8", bytes.NewReader(jsonBytes)) req.Header.Set("Content-Type", "application/json") req.Header.Set("Accept", "application/json")

client := &http.Client{Timeout: 15 * time.Second} resp, err := client.Do(req) if err != nil { log.Fatal(err) } defer resp.Body.Close()

if resp.StatusCode < 200 || resp.StatusCode >= 300 { log.Printf("HTTP error: %d", resp.StatusCode) } body, _ := io.ReadAll(resp.Body) fmt.Println(string(body))

发送表单数据(x-www-form-urlencoded)

类似HTML表单提交,适用于登录、搜索等场景。使用url.Values编码,Header自动匹配。

  • url.Values{"key": {"value"}}构造数据,调用.Encode()生成字符串
  • Body用strings.NewReader()包装编码后的字符串
  • Header设为application/x-www-form-urlencoded
示例代码:
form := url.Values{}
form.Set("email", "test@example.com")
form.Set("token", "abc123")

req, _ := http.NewRequest("POST", "https://www./link/1e2d681b253cb12c33f7b4823af01d79", strings.NewReader(form.Encode())) req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

resp, err := http.DefaultClient.Do(req) if err != nil { log.Fatal(err) } defer resp.Body.Close() // 后续读取响应...

处理重定向与错误响应

默认情况下http.Client会自动跟随301/302重定向。如需禁用或自定义逻辑,可设置CheckRedirect函数。

  • 设置Client.CheckRedirecthttp.ErrUseLastResponse可停止重定向
  • 手动检查resp.StatusCode判断是否成功(如仅接受200)
  • resp.Statusresp.Header获取状态文本和响应头信息
禁用重定向示例:
client := &http.Client{
    CheckRedirect: func(req *http.Request, via []*http.Request) error {
        return http.ErrUseLastResponse // 停止跳转,返回当前响应
    },
    Timeout: 10 * time.Second,
}