Enable streaming request when posting or putting #168

Closed
shivawu opened this Issue Oct 1, 2012 · 6 comments

Projects

None yet

3 participants

@shivawu
shivawu commented Oct 1, 2012

The original net/http enables streaming request by body_stream in request class,
i think it would be a great feature to also enable it here.

I use the following code myself to enable it outside httparty,
it's just plain hack, and really simple to implement, 4 lines of code

class HTTParty::Request
    alias :super_setup_raw_request :setup_raw_request

    def body_stream
        options[:body_stream]
    end
    def setup_raw_request
        super_setup_raw_request
        @raw_request.body_stream = body_stream if body_stream
    end
end

then I can just do

HTTParty.put(some_host, :body_stream => File.open(xxx))

to perform a streaming request which cost less memory to store the data.

@jnunemaker
Owner

How do you actually use it then?

@shivawu
shivawu commented Oct 1, 2012

When putting or posting large data, like 500Mb or even more,
streaming request enables a pipeline between data source and request,
and will cost less memory.

With this piece of code, i can just do
HTTParty.put(some_host, :body_stream => FIle.open(xxx))

在 2012-10-1,下午9:54, John Nunemaker 写道:

How do you actually use it then?


Reply to this email directly or view it on GitHub.

@jnunemaker
Owner

Yep, understood why, just wanted to see how.

@shivawu
shivawu commented Oct 1, 2012

apology for my poor english, i was not sure which did you mean, so i answered both :-)

it seems body_stream does not have so many documentations, it took me some time to find it.
and i'm not sure if there're other kinds of convenience it can provide.

@klobuczek
Contributor

I just packaged this change as a pull request. Regarding your question how it is used. In rails controller I use it the following way:
HTTParty.post('http://someserver', body_stream: params[:file],
headers: {'Transfer-Encoding' => 'chunked', 'Content-Type' => 'application/json'}) }

@jnunemaker
Owner

This is in master now.

@jnunemaker jnunemaker closed this Mar 14, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment