Page updated Jan 16, 2024

Fetch data

GET requests

To make a GET request use Amplify.API.get:

1Future<void> getTodo() async {
2 try {
3 final restOperation = Amplify.API.get('todo');
4 final response = await restOperation.response;
5 print('GET call succeeded: ${response.decodeBody()}');
6 } on ApiException catch (e) {
7 print('GET call failed: $e');
8 }
9}

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:

1exports.handler = function(event, context, callback) {
2 console.log(event.queryStringParameters);
3 console.log('body: ', event.body);
4}

In case you do not have it already, alternatively, you can update your backend file which is located at amplify/backend/function/[your-lambda-function]/src/app.js with the middleware:

1const awsServerlessExpressMiddleware = require('aws-serverless-express/middleware');
2app.use(awsServerlessExpressMiddleware.eventContext());

Accessing Query Parameters with Serverless Express

In your request handler use req.apiGateway.event or req.query:

1app.get('/todo', function(req, res) {
2 const query = req.query;
3 // or
4 // const query = req.apiGateway.event.queryStringParameters
5 res.json({
6 event: req.apiGateway.event, // to view all event data
7 query: query
8 });
9});

Then you can use query parameters in your path as follows:

1Future<void> getTodo() async {
2 try {
3 final restOperation = Amplify.API.get(
4 'todo',
5 queryParameters: {'q': 'test'},
6 );
7 final response = await restOperation.response;
8 print('GET call succeeded: ${response.decodeBody()}');
9 } on ApiException catch (e) {
10 print('GET call failed: $e');
11 }
12}