Skip to content

Support for streaming requests with httr2 #273

Description

@Aariq

It seems that currently vcr doesn't record cassettes for requests made with httr2::req_perform_stream() or httr2::req_perform_connection(). Maybe this is a limitation of how streaming requests work—I'm not sure.

library(httr2)
vcr::use_cassette("req_perform_stream", {
  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)
  resp
})
#> Got 32768 bytes
#> Got 32768 bytes
#> Got 32768 bytes
#> Got 1696 bytes
#> Warning: Empty cassette (req_perform_stream) deleted; consider the following:
#>  - If an error occurred resolve that first, then check:
#>  - vcr only supports crul, httr & httr2; requests w/ curl, download.file, etc. are not supported
#>  - If you are using crul/httr/httr2, are you sure you made an HTTP request?
#> <vcr - Cassette> req_perform_stream
#>   Record method: once
#>   Serialize with: yaml
#>   Persist with: FileSystem
#>   Re-record interval (s): 
#>   Clean outdated interactions?: FALSE
#>   update_content_length_header: FALSE
#>   allow_playback_repeats: FALSE
#>   allow_unused_http_interactions: 
#>   exclusive: 
#>   preserve_exact_body_bytes: FALSE

vcr::use_cassette("req_perform_connection", {
  req <- request(example_url()) |>
    req_url_path("/stream-bytes/32768")
  resp <- req_perform_connection(req)
  
  length(resp_stream_raw(resp, kb = 16))
  length(resp_stream_raw(resp, kb = 16))
})
#> Warning: Empty cassette (req_perform_connection) deleted; consider the following:
#>  - If an error occurred resolve that first, then check:
#>  - vcr only supports crul, httr & httr2; requests w/ curl, download.file, etc. are not supported
#>  - If you are using crul/httr/httr2, are you sure you made an HTTP request?
#> <vcr - Cassette> req_perform_connection
#>   Record method: once
#>   Serialize with: yaml
#>   Persist with: FileSystem
#>   Re-record interval (s): 
#>   Clean outdated interactions?: FALSE
#>   update_content_length_header: FALSE
#>   allow_playback_repeats: FALSE
#>   allow_unused_http_interactions: 
#>   exclusive: 
#>   preserve_exact_body_bytes: FALSE

Created on 2025-01-21 with reprex v2.1.1

It would be nice to either have this "just work" or get a more customized warning for streaming requests. This comes from me re-factoring code originally using curl and comments in the tests mention that original stubs were created manually, so perhaps that is an option I should explore?

Metadata

Metadata

Assignees

Labels

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions