Fetch data
To invoke an endpoint, you need to set input object with required apiName option and optional headers, queryParams, and body options. API status code response > 299 are thrown as an ApiError instance. The error instance provides name and message properties parsed from the response.
GET requests
import { get } from 'aws-amplify/api';
async function getItem() { try { const restOperation = get({ apiName: 'myRestApi', path: 'items' options: { retryStrategy: { strategy: 'no-retry' // Overrides default retry strategy }, } }); const response = await restOperation.response; console.log('GET call succeeded: ', response); } catch (error) { console.log('GET call failed: ', JSON.parse(error.response.body)); }}The retryStrategy can be configured with:
no-retry: Single attempt, fails immediately on errorjittered-exponential-backoff: Default strategy that retries with increasing delays, maximum 3 attempts
Accessing response payload
You can consume the response payload by accessing the body property of the response object. Depending on the use case and the content type of the body, you can consume they payload in string, blob, or JSON.
// ...const { body } = await restOperation.response;// consume as a string:const str = await body.text();// OR consume as a blob:const blob = await body.blob();// OR consume as a JSON:const json = await body.json();Access HTTP response from errors
The REST API handler may throw an ApiError error instance. If the error is caused by an HTTP response with a non-2xx status code, the error instance will provide a response property. The response property contains following
properties:
statusCode: HTTP status codeheaders: HTTP response headersbody: HTTP response body as a string
The following example shows how to access the HTTP response from an ApiError instance, so that you can handle the error response from your REST API endpoint:
import { ApiError, get } from 'aws-amplify/api';
try { const restOperation = get({ apiName: 'myRestApi', path: 'items' }); await restOperation.response;} catch (error) { if (error instanceof ApiError) { if (error.response) { const { statusCode, headers, body } = error.response; console.error(`Received ${statusCode} error response with payload: ${body}`); } // Handle API errors not caused by HTTP response. } // Handle other errors.}