VcrResponse | R Documentation |
Custom vcr http response object
status
the status of the response
headers
the response headers
body
the response body
http_version
the HTTP version
opts
a list
adapter_metadata
Additional metadata used by a specific VCR adapter
hash
a list
disk
a boolean
new()
Create a new VcrResponse object
VcrResponse$new( status, headers, body, http_version, opts, adapter_metadata = NULL, disk )
status
the status of the response
headers
the response headers
body
the response body
http_version
the HTTP version
opts
a list
adapter_metadata
Additional metadata used by a specific VCR adapter
disk
boolean, is body a file on disk
A new VcrResponse
object
print()
print method for the VcrResponse
class
VcrResponse$print(x, ...)
x
self
...
ignored
to_hash()
Create a hash
VcrResponse$to_hash()
a list
from_hash()
Get a hash back to an R list
VcrResponse$from_hash(hash)
hash
a list
an VcrResponse
object
update_content_length_header()
Updates the Content-Length response header so that it is accurate for the response body
VcrResponse$update_content_length_header()
no return; modifies the content length header
get_header()
Get a header by name
VcrResponse$get_header(key)
key
(character) header name to get
the header value (if it exists)
edit_header()
Edit a header
VcrResponse$edit_header(key, value = NULL)
key
(character) header name to edit
value
(character) new value to assign
no return; modifies the header in place
delete_header()
Delete a header
VcrResponse$delete_header(key)
key
(character) header name to delete
no return; the header is deleted if it exists
content_encoding()
Get the content-encoding header value
VcrResponse$content_encoding()
(character) the content-encoding value
is_compressed()
Checks if the encoding is one of "gzip" or "deflate"
VcrResponse$is_compressed()
logical
clone()
The objects of this class are cloneable with this method.
VcrResponse$clone(deep = FALSE)
deep
Whether to make a deep clone.
## Not run:
vcr_configure(dir = tempdir())
# basic example of VcrResponse use
url <- "https://google.com"
(cli <- crul::HttpClient$new(url = url))
(res <- cli$get("get", query = list(q = "stuff")))
(x <- VcrResponse$new(res$status_http(), res$response_headers,
res$parse("UTF-8"), res$response_headers$status))
x$body
x$status
x$headers
x$http_version
x$to_hash()
x$from_hash(x$to_hash())
# update content length header
## example 1
### content-length header present, but no change
url <- "https://fishbase.ropensci.org"
cli <- crul::HttpClient$new(url = url, headers = list(`Accept-Encoding` = '*'))
res <- cli$get("species/34")
x <- VcrResponse$new(res$status_http(), res$response_headers,
res$parse("UTF-8"), res$response_headers$status)
x$headers$`content-length`
x$update_content_length_header()
x$headers$`content-length`
## example 2
### no content-length header b/c a transfer-encoding header is included
### and no content-length header allowed if transfer-encoding header
### used (via rfc7230)
url <- "https://google.com"
cli <- crul::HttpClient$new(url = url)
res <- cli$get()
x <- VcrResponse$new(res$status_http(), res$response_headers,
rawToChar(res$content), res$response_headers$status)
x$headers$`content-length` # = NULL
x$update_content_length_header() # no change, b/c header doesn't exist
x$headers$`content-length` # = NULL
## example 3
### content-length header present, and does change
body <- " Hello World "
x <- VcrResponse$new(200, list('content-length'=nchar(body)),
body, "HTTP/2")
x$headers$`content-length` # = 13
x$body <- gsub("^\\s|\\s$", "", x$body)
x$headers$`content-length` # = 13
x$update_content_length_header()
x$headers$`content-length` # = 11
# check if body is compressed
url <- "https://fishbase.ropensci.org"
(cli <- crul::HttpClient$new(url = url))
(res <- cli$get("species/3"))
res$response_headers
(x <- VcrResponse$new(res$status_http(), res$response_headers,
res$parse("UTF-8"), res$response_headers$status))
x$content_encoding()
x$is_compressed()
# with disk
url <- "https://google.com"
(cli <- crul::HttpClient$new(url = url))
f <- tempfile()
(res <- cli$get("get", query = list(q = "stuff"), disk = f))
(x <- VcrResponse$new(res$status_http(), res$response_headers,
f, res$response_headers$status, disk = TRUE))
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.