resp_body_raw: Extract body from response

View source: R/resp-body.R

resp_body_rawR Documentation

Extract body from response

Description

  • resp_body_raw() returns the raw bytes.

  • resp_body_string() returns a UTF-8 string.

  • resp_body_json() returns parsed JSON.

  • resp_body_html() returns parsed HTML.

  • resp_body_xml() returns parsed XML.

  • resp_has_body() returns TRUE if the response has a body.

resp_body_json() and resp_body_xml() check that the content-type header is correct; if the server returns an incorrect type you can suppress the check with check_type = FALSE. These two functions also cache the parsed object so the second and subsequent calls are low-cost.

Usage

resp_body_raw(resp)

resp_has_body(resp)

resp_body_string(resp, encoding = NULL)

resp_body_json(resp, check_type = TRUE, simplifyVector = FALSE, ...)

resp_body_html(resp, check_type = TRUE, ...)

resp_body_xml(resp, check_type = TRUE, ...)

Arguments

resp

A httr2 response object, created by req_perform().

encoding

Character encoding of the body text. If not specified, will use the encoding specified by the content-type, falling back to UTF-8 with a warning if it cannot be found. The resulting string is always re-encoded to UTF-8.

check_type

Check that response has expected content type? Set to FALSE to suppress the automated check

simplifyVector

Should JSON arrays containing only primitives (i.e. booleans, numbers, and strings) be caused to atomic vectors?

...

Other arguments passed on to jsonlite::fromJSON() and xml2::read_xml() respectively.

Value

  • resp_body_raw() returns a raw vector.

  • resp_body_string() returns a string.

  • resp_body_json() returns NULL, an atomic vector, or list.

  • resp_body_html() and resp_body_xml() return an xml2::xml_document

Examples

resp <- request("https://httr2.r-lib.org") |> req_perform()
resp

resp |> resp_has_body()
resp |> resp_body_raw()
resp |> resp_body_string()

if (requireNamespace("xml2", quietly = TRUE)) {
  resp |> resp_body_html()
}

httr2 documentation built on Sept. 14, 2024, 1:08 a.m.