Skip to contents

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().

Usage

resp_tidy(resp)

Arguments

resp

(httr2_response) A single httr2::response() object (as returned by httr2::req_perform()).

Value

The extracted and cleaned response, or NULL if resp is NULL. By default, the response is processed with resp_body_auto(). If the request includes a resp_tidy policy (set via req_tidy_policy()), that policy's function and arguments are used instead.

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"),
  tidy_policy_prepare(httr2::resp_body_json)
)
# In practice, the request is attached automatically when the response is
# fetched with httr2::req_perform() or req_perform_opinionated().
resp$request <- req
resp_tidy(resp)
#> $a
#> [1] 1
#> 
#> $b
#> [1] "hello"
#>