Assembles the request, applies req_defaults(), performs it, and caches the
result if it succeeded.
Usage
get_json(
base_url,
path = NULL,
query = list(),
source = "API",
timeout = 15,
max_tries = 3,
headers = NULL,
throttle = NULL,
secret_query = NULL
)Arguments
- base_url
The service's base URL.
- path
An optional path appended to
base_url.- query
A named list of query parameters. Blank values are dropped.
- source
A friendly label for the service.
- timeout
Seconds before the request is abandoned.
- max_tries
Total attempts, including the first.
- headers
A named list of headers, all marked sensitive.
- throttle
An optional throttle spec. See
req_defaults().- secret_query
A named list of query-string credentials, for a service that has no header form. Deliberately not part of the cache key, so it suits a credential that raises a rate limit and not one that changes the response. See
redact_secrets().
Value
An envelope. See envelope().
Examples
# Mocked so the example runs offline. A real call drops the
# with_mocked_responses() wrapper.
cache_reset()
breaker_reset()
httr2::with_mocked_responses(
list(httr2::response(
status_code = 200,
headers = list(`content-type` = "application/json"),
body = charToRaw('{"hits":[{"symbol":"BRCA1"}]}')
)),
get_json(
"https://mygene.info/v3",
path = "query",
query = list(q = "BRCA1", species = "human"),
source = "MyGene"
)$data$hits[[1]]$symbol
)
#> [1] "BRCA1"