if (FALSE) {
library(vcr)
library(crul)
vcr_configure(dir = tempdir())
use_cassette(name = "apple7", {
cli <- HttpClient$new(url = "https://hb.opencpu.org")
resp <- cli$get("get")
})
readLines(file.path(tempdir(), "apple7.yml"))
# preserve exact body bytes - records in base64 encoding
use_cassette("things4", {
cli <- crul::HttpClient$new(url = "https://hb.opencpu.org")
bbb <- cli$get("get")
}, preserve_exact_body_bytes = TRUE)
## see the body string value in the output here
readLines(file.path(tempdir(), "things4.yml"))
# cleanup
unlink(file.path(tempdir(), c("things4.yml", "apple7.yml")))
# with httr
library(vcr)
library(httr)
vcr_configure(dir = tempdir(), log = TRUE, log_opts = list(file = file.path(tempdir(), "vcr.log")))
use_cassette(name = "stuff350", {
res <- GET("https://hb.opencpu.org/get")
})
readLines(file.path(tempdir(), "stuff350.yml"))
use_cassette(name = "catfact456", {
res <- GET("https://catfact.ninja/fact")
})
# record mode: none
library(crul)
vcr_configure(dir = tempdir())
## make a connection first
conn <- crul::HttpClient$new("https://eu.httpbin.org")
## this errors because 'none' disallows any new requests
# use_cassette("none_eg", (res2 <- conn$get("get")), record = "none")
## first use record mode 'once' to record to a cassette
one <- use_cassette("none_eg", (res <- conn$get("get")), record = "once")
one; res
## then use record mode 'none' to see it's behavior
two <- use_cassette("none_eg", (res2 <- conn$get("get")), record = "none")
two; res2
}
Run the code above in your browser using DataLab