httr2 provides two methods for performing requests: httr2::req_perform(),
which returns a single httr2::response() object, and
httr2::req_perform_iterative(), which returns a list of httr2::response()
objects. This function automatically determines whether a single response or
multiple responses have been returned, and parses the responses
appropriately.
Usage
resp_parse(resps, ...)
# Default S3 method
resp_parse(
resps,
...,
arg = rlang::caller_arg(resps),
call = rlang::caller_env()
)
# S3 method for class 'httr2_response'
resp_parse(resps, ..., response_parser = resp_tidy)Arguments
- resps
(
httr2_response,nectar_responses, orlist) A singlehttr2::response()object (as returned byhttr2::req_perform()) or a list of such objects (as returned byreq_perform_opinionated()orhttr2::req_perform_iterative()).- ...
Additional arguments passed on to the
response_parserfunction (in addition toresps).- arg
(
length-1 character) An argument name as a string. This argument will be mentioned in error messages as the input that is at the origin of a problem.- call
(
environment) The environment from which a function was called, e.g.rlang::caller_env()(the default). The environment will be mentioned in error messages as the source of the error. This argument is particularly useful for functions that are intended to be called as utilities inside other functions.- response_parser
(
function) A function to parse the server response (resp). Defaults tohttr2::resp_body_json(), since JSON responses are common. Set this toNULLto return the raw response fromhttr2::req_perform().
Value
The response parsed by the response_parser. If resps was a list,
the parsed responses are concatenated when possible. Unlike
httr2::resps_data, this function does not concatenate raw vector
responses.
Examples
resp <- httr2::response_json(body = list(a = 1, b = "hello"))
resp_parse(resp, response_parser = httr2::resp_body_json)
#> $a
#> [1] 1
#>
#> $b
#> [1] "hello"
#>
resps <- list(
httr2::response_json(body = list(list(id = 1), list(id = 2))),
httr2::response_json(body = list(list(id = 3), list(id = 4)))
)
resp_parse(resps, response_parser = httr2::resp_body_json)
#> [[1]]
#> [[1]]$id
#> [1] 1
#>
#>
#> [[2]]
#> [[2]]$id
#> [1] 2
#>
#>
#> [[3]]
#> [[3]]$id
#> [1] 3
#>
#>
#> [[4]]
#> [[4]]$id
#> [1] 4
#>
#>