fetchJson
Fetches a URL and returns its parsed JSON body, retrying transient failures.
Retried: network errors, 5xx and 429 responses. Not retried, and thrown at once: other
4xx responses, a body that is not valid JSON (a SyntaxError) and an aborted request.
After the last attempt, the last error is thrown: an HttpError (with status and
url) for a bad status, or whatever fetch rejected with.
Pass a URL or a string: a Request with a body cannot be sent twice, so it can’t be retried.
init.signal cancels the whole call: the running request, and the wait before the next one.
Available since v3.1.11
Import
Section titled “Import”Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
input | RequestInfo | URL | URL or `Request` object passed to `fetch` |
init | RequestInit | Optional `RequestInit` options passed to `fetch` (optional) |
options | FetchJsonOptions | Retry configuration — see FetchJsonOptions |
Returns
Section titled “Returns”Promise<T> — The parsed JSON body
Examples
Section titled “Examples”Fetch JSON, retrying transient failures
Section titled “Fetch JSON, retrying transient failures”A 503 is retried; once the server answers, the parsed body is returned.
A 404 is not retried
Section titled “A 404 is not retried”Client errors fail at once with an HttpError carrying the status.
Related Types
Section titled “Related Types”FetchJsonOptions
Section titled “FetchJsonOptions”Options for fetchJson.
