Learn R Programming

vcr (version 0.2.6)

VcrResponse: The response of an HTTPInteraction

Description

The response of an HTTPInteraction

Arguments

status

the status of the response

headers

the response headers

body

the response body

http_version

the HTTP version

adapter_metadata

Additional metadata used by a specific VCR adapter

Details

Methods

to_hash()

Create a hash.

from_hash(hash)

Get a hash back to an R list.

update_content_length_header()

Updates the Content-Length response header so that it is accurate for the response body

get_header(key)

get a header by key (i.e., name) - key: header name to get

edit_header(key, value = NULL)

edit header - key: header name to edit - value: new value to assign

delete_header(key)

delete a header - key: header name to delete

content_encoding()

get content encoding

is_compressed()

Checks if the type of encoding is one of "gzip" or "deflate"

Examples

Run this code
# NOT RUN {
vcr_configure(dir = tempdir())


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
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`
x$update_content_length_header() # no change, b/c header doesn't exist


# 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()
# }

Run the code above in your browser using DataLab