Closed
Description
Currently only 3 types can specify automatically the ContentLength of the body of an http.Request: - bytes.Reader - bytes.Buffer - strings.Reader In these cases, the *http.Request automatically has the ContentLength field set by the value of the Len method of these three types. A user using the http package with any other kind of io.Reader must manually call NewRequest, then set the ContentLength themselves, and then call http.Client.Do. If instead, if http.NewRequest was modified to support passing an io.Reader with a (for example) Size() method, and then set the ContentLength to the value returned from that function, users could avoid having to set the ContentLength field of the request themselves. This would allow the http.Post() and http.Client.Post() helper methods to be more used in more situations, if passed such an io.Reader. The bigger benefit would be for external packages which use http though. A situation has arisen with github package that makes http requests to the github API where uploads must be sent a Content-Length, see: https://groups.google.com/forum/#!topic/golang-nuts/Hipcz658QQ8 Currently the easiest way to upload files successfully is to load the entire file into a []byte in memory, create a bytes.Buffer or bytes.Reader from it, and pass that to the package functions. With the proposed feature, an "augmented" os.File could be used instead, saving a lot of memory and time for large files. Although it would be possible to modify the go-github package to get a size (somehow) for the data in the io.Reader and then set the Content-Length itself, by providing this functionality in the standard library, then all packages which wrap the http package could benefit.