API responses generally follow a structured format. Use this function to
extract the relevant portion of a response, and wrangle it into a desired
format. This function is most useful when the response was fetched with a
request that includes a tidying policy defined via req_tidy_policy().
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()).
Value
The extracted and cleaned response, or, for a list of responses,
those responses cleaned then concatenated via httr2::resps_data(). By
default, the response is processed with resp_body_auto().
See also
resp_tidy_json() for an opinionated response parser for JSON
responses, resp_body_auto() (etc) for a family of response parsers that
attempts to automatically select the appropriate parser based on the
response content type, httr2::resp_body_raw() (etc) for the underlying
httr2 response parsers, and resp_parse() for an alternative approach to
dealing with responses (particularly useful if the request does not include
a resp_tidy policy).
Examples
# Without a tidy policy, resp_tidy() uses resp_body_auto()
resp <- httr2::response_json(body = list(a = 1, b = "hello"))
resp_tidy(resp)
#> $a
#> [1] 1
#>
#> $b
#> [1] "hello"
#>
# With a tidy policy, resp_tidy() uses the policy's tidy function.
req <- req_tidy_policy(
httr2::request("https://example.com"),
httr2::resp_body_json
)
# In practice, the request is attached automatically when the response is
# fetched with req_perform() or req_perform_opinionated().
resp$request <- req
resp_tidy(resp)
#> $a
#> [1] 1
#>
#> $b
#> [1] "hello"
#>