You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Go实现支持可选请求体的HTTP请求函数及nil参数 panic修复方案

解决方案:Go HTTP工具包传nil请求体触发Panic问题

问题根源

  1. 提前关闭响应Body:SendRequest函数中用defer httpResponse.Body.Close()提前关闭了响应Body,导致后续业务代码调用DecodeResponseBody时,读取已关闭的Body引发错误甚至Panic。
  2. 请求体参数类型受限:函数参数使用*bytes.Buffer,虽传nil不会直接导致http.NewRequest报错,但限制了请求体类型,不符合Go标准库惯用设计。

修复步骤及代码示例

1. 重构httputil工具包

package httputil

import (
	"errors"
	"io"
	"net/http"
)

// SendRequest 发送HTTP请求,响应Body由调用方负责关闭
func SendRequest(method string, url string, body io.Reader) (*http.Response, error) {
	client := &http.Client{}
	req, err := http.NewRequest(method, url, body)
	if err != nil {
		return nil, err
	}

	httpResponse, err := client.Do(req)
	if err != nil {
		return nil, err
	}

	// 移除此处的defer关闭,交由调用方管理Body生命周期
	return httpResponse, nil
}

func DecodeResponseBody(httpResponse *http.Response) ([]byte, error) {
	// 增加nil响应的容错判断
	if httpResponse == nil {
		return nil, errors.New("响应对象为空")
	}

	body, err := io.ReadAll(httpResponse.Body)
	if err != nil {
		return nil, err
	}

	if httpResponse.StatusCode != http.StatusOK {
		return nil, errors.New(string(body))
	}

	return body, nil
}

2. 修正业务代码调用逻辑

package service

import (
	"encoding/json"
	"fmt"
	"net/http"
	"your-project-path/httputil" // 替换为实际包路径
)

func YourBusinessMethod(url string) (string, error) {
	// 传入nil作为请求体(适配DELETE/GET等无请求体的HTTP方法)
	httpResponse, err := httputil.SendRequest(http.MethodDelete, url, nil)
	if err != nil {
		return "", err
	}
	// 获取响应后立即defer关闭Body,确保资源正常释放
	defer httpResponse.Body.Close()

	var response string
	responseBody, err := httputil.DecodeResponseBody(httpResponse)
	if err != nil {
		return "", err
	}

	if err := json.Unmarshal(responseBody, &response); err != nil {
		return "", fmt.Errorf("解码响应失败: %v", err)
	}

	return response, nil
}

关键说明

  • Body生命周期管理:Go标准库要求客户端必须自行关闭响应Body,将关闭逻辑移到调用方,避免工具包提前关闭导致后续读取失败。
  • 参数类型优化:改用io.Reader作为请求体参数,既支持各类请求体实现,也允许传入nil(符合无请求体的HTTP方法规范)。
  • 容错增强:在DecodeResponseBody中增加nil响应判断,提升代码健壮性。

内容的提问来源于stack exchange,提问作者hafis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 23:30:13