req_perform_stream: Perform a request and handle data as it streams back

View source: R/req-perform-stream.R

req_perform_streamR Documentation

Perform a request and handle data as it streams back

Description

After preparing a request, call req_perform_stream() to perform the request and handle the result with a streaming callback. This is useful for streaming HTTP APIs where potentially the stream never ends.

Usage

req_perform_stream(req, callback, timeout_sec = Inf, buffer_kb = 64)

Arguments

req

A request.

callback

A single argument callback function. It will be called repeatedly with a raw vector whenever there is at least buffer_kb worth of data to process. It must return TRUE to continue streaming.

timeout_sec

Number of seconds to processs stream for.

buffer_kb

Buffer size, in kilobytes.

Value

An HTTP response.

Examples

show_bytes <- function(x) {
  cat("Got ", length(x), " bytes\n", sep = "")
  TRUE
}
resp <- request(example_url()) |>
  req_url_path("/stream-bytes/100000") |>
  req_perform_stream(show_bytes, buffer_kb = 32)

httr2 documentation built on Nov. 14, 2023, 5:08 p.m.