问题描述: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