Fetch data
GET requests
To make a GET request use Amplify.API.get
:
Future<void> getTodo() async { try { final restOperation = Amplify.API.get('todo'); final response = await restOperation.response; print('GET call succeeded: ${response.decodeBody()}'); } on ApiException catch (e) { print('GET call failed: $e'); }}
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);}
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:
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('/todo', 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:
Future<void> getTodo() async { try { final restOperation = Amplify.API.get( 'todo', queryParameters: {'q': 'test'}, ); final response = await restOperation.response; print('GET call succeeded: ${response.decodeBody()}'); } on ApiException catch (e) { print('GET call failed: $e'); }}