This is a creation in Article, where the information may have evolved or changed.
Reference: https://stackoverflow.com/questions/31337891/net-http-http-contentlength-222-with-body-length-0
Description: An error occurred when sending a POST request using the Golang HTTP packet, similar to http:contentlength=355 with Body length 0. The main idea is that conlentlength set a certain length, but when reading the body, found that there is no content in the body.
Problem Description:
Func Post (URLstringA form URL. Values, cl *http. Client) ([]byte, error) {body:=form. Encode ()
//req, err: = Http.newrequest ("POST", URL, strings. Newreader (body))//Execution error contentlength=355 with body length 0 forI: =0; I <Ten; i++{ req, err: = http. Newrequest ("POST", URL, strings. Newreader (body))//perform the correctifErr! =Nil {log. Error (ERR)returnnil, err} req. Header.set ("user-agent", UA) req. Header.set ("Content-type","application/x-www-form-urlencoded") RSP, err:=CL. Do (req)ifErr = =Nil {defer RSP. Body.close () b, err:=Ioutil. ReadAll (RSP. Body)ifErr! =Nil {log. Error (ERR)returnnil, err}returnB, nil}if!Iserrorproxy (err) {returnnil, err} log. Errorf ("Proxy is slow or down") time. Sleep (6*Time . Second)}returnNil, FMT. Errorf ("After tries error:%v", err)}
The cause of the problem req creation cannot be placed outside the for loop because the Do method closes the body read in req, so when the same req is passed to do again, the body's Read method is closed and the body content cannot be read. The above is personal understanding, if there are errors, please advise:-)
Workaround: Each call to do creates a req.