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 getTodo() { try { const restOperation = get({ apiName: 'todo-api', path: '/todo' }); const response = await restOperation.response; console.log('GET call succeeded: ', response); } catch (e) { console.log('GET call failed: ', JSON.parse(e.response.body)); }}
Accessing query parameters & body in Lambda proxy function
To learn more about Lambda Proxy Integration, please visit Amazon API Gateway Developer Guide.
If you are using a REST API which is generated with Amplify CLI, your backend is created with Lambda Proxy Integration, and you can access your query parameters & body within your Lambda function via the event object:
exports.handler = function(event, context, callback) { console.log(event.queryStringParameters); console.log('body: ', event.body);};
Alternatively, you can update your backend file which is located at amplify/backend/function/[your-lambda-function]/app.js
with the middleware:
const awsServerlessExpressMiddleware = require('aws-serverless-express/middleware');app.use(awsServerlessExpressMiddleware.eventContext());
Accessing Query Parameters with Serverless Express
In your request handler use req.apiGateway.event
or req.query
:
app.get('/items', function(req, res) { const query = req.query; // or // const query = req.apiGateway.event.queryStringParameters res.json({ event: req.apiGateway.event, // to view all event data query: query });});
Then you can use query parameters in your path as follows:
async function getTodo() { try { const restOperation = get({ apiName: 'todo-api', path: '/todo', options: { queryParams: { id: '123' } } }); const response = await restOperation.response; console.log('GET call succeeded: ', response); } catch (e) { console.log('GET call failed: ', JSON.parse(e.response.body)); }}
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: 'todo-api', path: '/todo' }); 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.}