Skip to content

proposal: net/http: allow NewRequest to type assert on io.Readers with a Len method #30118

Closed
@kortschak

Description

@kortschak

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 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.

See also https://groups.google.com/d/topic/golang-nuts/NdeMVAamx8E/discussion

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions