crul (version 0.9.0)

HttpResponse: Base response object

Description

Base response object

Arguments

method

(character) HTTP method

url

(character) A url, required

opts

(list) curl options

handle

A handle

status_code

(integer) status code

request_headers

(list) request headers, named list

response_headers

(list) response headers, named list

response_headers_all

(list) all response headers, including intermediate redirect headers, unnamed list of named lists

modified

(character) modified date

times

(vector) named vector

content

(raw) raw binary content response

request

request object, with all details

Usage

HttpResponse$new(method, url, opts, handle, status_code, request_headers,
   response_headers, response_headers_all, times, content,
   request)

Methods

parse(encoding = NULL, ...)

Parse the raw response content to text

  • encoding: (character) A character string describing the current encoding. If left as NULL, we attempt to guess the encoding. Passed to from parameter in iconv

  • ...: additional parameters passed on to iconv (options: sub, mark, toRaw). See ?iconv for help

  • returns: character

success()

Was status code less than or equal to 201.

  • returns: boolean

status_http(verbose = FALSE)

Get HTTP status code, message, and explanation

  • returns: object of class "http_code", a list with slots for status_code, message, and explanation

raise_for_status()

Check HTTP status and stop with appropriate HTTP error code and message if >= 300. otherwise use httpcode. If you have fauxpas installed we use that.

  • returns: stop or warn with message

raise_for_ct(type, charset = NULL, behavior = "stop")

Check response content-type; stop or warn if not matched. Parameters:

  • type: (character) a mime type to match against; see mime::mimemap for allowed values

  • charset: (character) if a charset string given, we check that it matches the charset in the content type header. default: NULL

  • behavior: (character) one of stop (default) or warning

raise_for_ct_html(charset = NULL, behavior = "stop")

Check that the response content-type is text/html; stop or warn if not matched. Parameters: see raise_for_ct()

raise_for_ct_json(charset = NULL, behavior = "stop")

Check that the response content-type is application/json; stop or warn if not matched. Parameters: see raise_for_ct()

raise_for_ct_xml(charset = NULL, behavior = "stop")

Check that the response content-type is application/xml; stop or warn if not matched. Parameters: see raise_for_ct()

See Also

content-types

Examples

Run this code
# NOT RUN {
x <- HttpResponse$new(method = "get", url = "https://httpbin.org")
x$url
x$method

x <- HttpClient$new(url = 'https://httpbin.org')
(res <- x$get('get'))
res$request_headers
res$response_headers
res$parse()
res$status_code
res$status_http()
res$status_http()$status_code
res$status_http()$message
res$status_http()$explanation
res$success()

x <- HttpClient$new(url = 'https://httpbin.org/status/404')
(res <- x$get())
# res$raise_for_status()

x <- HttpClient$new(url = 'https://httpbin.org/status/414')
(res <- x$get())
# res$raise_for_status()
# }

Run the code above in your browser using DataLab