go 踩坑报错处理http: ContentLength=XXXwith Body length 0

问题描述:go语言发送请求返回Post http://xxxxx.com(服务地址): http: ContentLength=1278 with Body length 0 

问题解决,代码结构大致如下图

//错误代码
//req结构体在for循坏外定义
req, err := http.NewRequest("POST", url, bytes.NewReader(data))

//这里是请求其他系统,诉求是请求失败后可以重试
for attempt := 0; attempt < maxRetry; attempt++ {
   resp, err := httpClient.Do(req)
    //...etc
}

查询了Stack Overflow,得到答案是第一次发送http请求失败后,第二次尝试的时候req的body被清空了。

符合自己的代码情况,看了日志也确实是第一次请求失败,后面再次发送请求返回的ContentLength=1278 with Body length 0 。

看源码可以知道,body是个io.Reader,第一次发送后,Request.Body已经被读并且stream被关闭了,所以再次请求的时候Body 就是0了,解决办法就是把定义req放入循环结构体内

io.Reader, and convert to io.ReaderCloser by ioutil.NopCloser. As @Cerise Limón said, the Request.Body had been read and the stream is closer, so when you Do() again, the Body Length is 0.

func Post(URL string, form url.Values, cl *http.Client) ([]byte, error) {
    requestBodyString := form.Encode()
    req, err := http.NewRequest("POST", URL, strings.NewReader(requestBodyString))
    // ...

    rsp, err := do(cl, req, requestBodyString)

    //...
    return b, nil
}

func do(cl *http.Client, req *http.Request, requestBodyString string)(*http.Response, error){
    rsp, err := cl.Do(req)
    for i := 0; IsErrProxy(err); i++ {
        log.Errorf("Proxy is slow or down ")
        time.Sleep(6 * time.Second)
        // reset Request.Body
        req.Body = ioutil.NopCloser(strings.NewReader(requestBodyString))
        rsp, err = cl.Do(&req)
        if err == nil{
            return rsp, nil
        }
        if i > 10 {

            return nil, fmt.Errorf("after %v tries error: %v", i, err)
        }
    }
    return rsp, err
}
//代码修改


//这里是请求其他系统,诉求是请求失败后可以重试
for attempt := 0; attempt < maxRetry; attempt++ {
//req结构体在定义在for循坏内
req, err := http.NewRequest("POST", url, bytes.NewReader(data))
   resp, err := httpClient.Do(req)
    //...etc
}



原答案地址:
https://stackoverflow.com/questions/31337891/net-http-http-contentlength-222-with-body-length-0

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

shaohjz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值