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
Currently NewRequesttype switches on *bytes.Buffer, *bytes.Reader and *strings.Reader to determine whether the body's content length is known and so can be used to set the ContentLength header. It would be helpful in some cases for other io.Readers that are aware of their length to be able to be used in the same way by adding an interface { Len() int } case.
In AusOcean code we have seen two bugs arising from this since we send requests with an internal io.Reader that is not one of the three blessed types. For whatever reason, likely a bug in dev_appengine.py (during CI testing) this results in an error from the request that is avoided if the data is first copied into a *bytes.Buffer.
The addition of a interface { Len() int } case looks like it can not be used in conjunction with GetBody without use of reflect, though I'm not sure how significant this is.
rsc
changed the title
proposal: net/http: allow ` to type assert on io.Readers with a Len method
proposal: net/http: allow NewRequest to type assert on io.Readers with a Len method
Feb 13, 2019
It seems unfortunate to hard-code use of an interface that can't describe a 2GB file on 32-bit systems.
Note that you can set req.ContentLength in the result from NewRequest if you do know the size (or prepare the request yourself entirely; NewRequest is just a slight helper).
Thanks, Russ. It's probably worth documenting the "workaround" more clearly since a few people have hit this. Careful thought leads to the correct approach, as noted in the linked thread, but often later than it should.
Currently
NewRequest
type switches on*bytes.Buffer
,*bytes.Reader
and*strings.Reader
to determine whether the body's content length is known and so can be used to set theContentLength
header. It would be helpful in some cases for otherio.Reader
s that are aware of their length to be able to be used in the same way by adding aninterface { Len() int }
case.In AusOcean code we have seen two bugs arising from this since we send requests with an internal
io.Reader
that is not one of the three blessed types. For whatever reason, likely a bug indev_appengine.py
(during CI testing) this results in an error from the request that is avoided if the data is first copied into a*bytes.Buffer
.The addition of a
interface { Len() int }
case looks like it can not be used in conjunction withGetBody
without use of reflect, though I'm not sure how significant this is.See also https://groups.google.com/d/topic/golang-nuts/NdeMVAamx8E/discussion
The text was updated successfully, but these errors were encountered: