Golang http Post error:http:contentlength=355 with Body length 0

Source: Internet
Author: User
Tags http post
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.