Code Monkey home page Code Monkey logo

Comments (7)

garyburd avatar garyburd commented on June 26, 2024

Here's one way to enable an io.Writer interface access to the request body using the Do method described elsewhere.

  • The client copies the body io.Reader to the connection using a custom version of io.Copy that prefers io.WriterTo over io.ReaderFrom.

  • Add function

      WriteToWrapper(writeTo func(io.Writer) error) io.Reader
    

    If WriteTo is called on the returned reader, then returned object calls the writeTo function do the work. If Read is called on the returned object, then the object creates a pipe and runs the writeTo function in a goroutine writing to the pipe.

  • Applications call Do(method, url, header, WriteToWrapper(func(w io.Writer) error { /* complex request generation code here */}))

from http.

james4k avatar james4k commented on June 26, 2024

I agree that an io.Writer for the request body could be the right approach. For the simple API it even makes sense:

func Get(dst io.Writer, srcURL string) (n int64, err error)

This would be familiar to anyone who's used io.Copy, and the burden of closing the response body is still with the library and not the user.

from http.

davecheney avatar davecheney commented on June 26, 2024

Thank you for your suggestion.

You would have seen the

http.Get(url string)([]byte, error)

Helper that exists now. As a high level method it has a lot of compromises and so I'm open to suggestions to improve it.

On question with your alternate Get function, what happens if the status is !200 ? Many status code can contain bodies, and as the status code is hidden from the caller, what should be done to avoid returning the text of a 404 page as a body?

Maybe at the highest level non 200 status codes should be converted to errors ?

On 27/07/2013, at 16:06, James Gray [email protected] wrote:

I agree that an io.Writer for the request body could be the right approach. For the simple API it even makes sense:

func Get(dst io.Writer, srcURL string) (n int64, err error)
This would be familiar to anyone who's used io.Copy, and the burden of closing the response body is still with the library and not the user.


Reply to this email directly or view it on GitHub.

from http.

james4k avatar james4k commented on June 26, 2024

The current Get function is still conceptually simpler, so I'm not sure if it should be replaced outright by an io.Writer variant. Perhaps it could exist as an alternative named something like GetTo.

I do think with an API this simple it makes sense to error on anything but a status of 200, but I may just not be thinking of an important case.

from http.

davecheney avatar davecheney commented on June 26, 2024

Just a suggestion. Comments and suggestions welcome. Also, if someone wants to write this code

type StatusError Status

func (s *StatusError) Error() string

which lets us convert a status code into an error, it would be much appreciated. This code should be in the http package.

from http.

davecheney avatar davecheney commented on June 26, 2024

Reopening because I didn't address Gary's request, only James' suggestion for http.Get.

from http.

garyburd avatar garyburd commented on June 26, 2024

io.Copy was changed to prefer io.WriterTo over io.ReaderFrom in tip. The WriteToWrapper trick should work everywhere that io.Copy is used including the net/http client.

from http.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.