Use the Content-Type header (extracted using httr2::resp_content_type())
of a response to automatically choose and apply a body parser, such as
httr2::resp_body_json() or resp_body_csv().
Arguments
- resp
(
httr2_response) A singlehttr2::response()object (as returned byhttr2::req_perform()).
Examples
resp_json <- httr2::response_json(body = list(a = 1, b = "hello"))
resp_body_auto(resp_json)
#> $a
#> [1] 1
#>
#> $b
#> [1] "hello"
#>
resp_csv <- httr2::response(
headers = list("Content-Type" = "text/csv"),
body = charToRaw("a,b\n1,2\n3,4")
)
resp_body_auto(resp_csv)
#> a b
#> 1 1 2
#> 2 3 4