mirror of
https://github.com/lostisland/faraday.git
synced 2025-10-04 00:02:03 -04:00
* Backwards-compatible * Allow adapters to provide response info to on_call block * Provide `stream_response` helper method
1.4 KiB
1.4 KiB
layout | title | permalink | hide | top_name | top_link | prev_name | prev_link |
---|---|---|---|---|---|---|---|
documentation | Streaming Responses | /usage/streaming | true | Usage | ./ | Customizing the Request | ./customize |
Sometimes you might need to receive a streaming response.
You can do this with the on_data
request option.
The on_data
callback is a receives tuples of chunk Strings, and the total
of received bytes so far.
This example implements such a callback:
# A buffer to store the streamed data
streamed = []
conn.get('/stream/10') do |req|
# Set a callback which will receive tuples of chunk Strings,
# the sum of characters received so far, and the response environment.
# The latter will allow access to the response status, headers and reason, as well as the request info.
req.options.on_data = Proc.new do |chunk, overall_received_bytes, env|
puts "Received #{overall_received_bytes} characters"
streamed << chunk
end
end
# Joins all response chunks together
streamed.join
The on_data
streaming is currently only supported by some adapters.
To see which ones, please refer to Awesome Faraday comparative table or check the adapter documentation.
Moreover, the env
parameter was only recently added, which means some adapters may only have partial support
(i.e. only chunk
and overall_received_bytes
will be passed to your block).