Perform a request and return a streaming connection
Source:R/req-perform-connection.R
req_perform_connection.Rd
Use req_perform_connection()
to perform a request if you want to stream the
response body. A response returned by req_perform_connection()
includes a
connection as the body. You can then use resp_stream_raw()
,
resp_stream_lines()
, or resp_stream_sse()
to retrieve data a chunk at a
time. Always finish up by closing the connection by calling
close(response)
.
This is an alternative interface to req_perform_stream()
that returns a
connection that you can use to pull the data, rather
than providing callbacks that the data is pushed to. This is useful if you
want to do other work in between handling inputs from the stream.
Arguments
- req
A httr2 request object.
- blocking
When retrieving data, should the connection block and wait for the desired information or immediately return what it has (possibly nothing)?
- verbosity
How much information to print? This is a wrapper around
req_verbose()
that uses an integer to control verbosity:0
: no output1
: show headers2
: show headers and bodies as they're streamed3
: show headers, bodies, curl status messages, and stream buffer management
Use
with_verbosity()
to control the verbosity of requests that you can't affect directly.
Examples
req <- request(example_url()) |>
req_url_path("/stream-bytes/32768")
resp <- req_perform_connection(req)
length(resp_stream_raw(resp, kb = 16))
#> [1] 16384
length(resp_stream_raw(resp, kb = 16))
#> [1] 16384
# When the stream has no more data, you'll get an empty result:
length(resp_stream_raw(resp, kb = 16))
#> [1] 0
# Always close the response when you're done
close(resp)
# You can loop until complete with resp_stream_is_complete()
resp <- req_perform_connection(req)
while (!resp_stream_is_complete(resp)) {
print(length(resp_stream_raw(resp, kb = 12)))
}
#> [1] 12288
#> [1] 12288
#> [1] 8192
close(resp)