You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// NewRequest returns a new Request given a method, URL, and optional body.//// If the provided body is also an io.Closer, the returned// Request.Body is set to body and will be closed by the Client// methods Do, Post, and PostForm, and Transport.RoundTrip.funcNewRequest(method, urlStrstring, body io.Reader) (*Request, error) {
u, err:=url.Parse(urlStr)
iferr!=nil {
returnnil, err
}
rc, ok:=body.(io.ReadCloser)
if!ok&&body!=nil {
rc=ioutil.NopCloser(body)
}
req:=&Request{
Method: method,
URL: u,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Header: make(Header),
Body: rc,
Host: u.Host,
}
ifbody!=nil {
switchv:=body.(type) {
case*bytes.Buffer:
req.ContentLength=int64(v.Len())
case*bytes.Reader:
req.ContentLength=int64(v.Len())
case*strings.Reader:
req.ContentLength=int64(v.Len())
}
}
returnreq, nil
}
The text was updated successfully, but these errors were encountered:
NewRequest has a few special cases for common types in the standard library with known lengths, but it doesn't do everything. You'll have to set your own lengths when it matters. Usually chunked encoding should be good enough.
mikioh
changed the title
Wrong ContentLength when use http.Request.Body as the third parameter of http.NewRequest()
net/http: Wrong ContentLength when use http.Request.Body as the third parameter of http.NewRequest()
Jul 2, 2015
Here's the net/http/request.go implementation:
The text was updated successfully, but these errors were encountered: