Name:
interface
Value:
Amplify has re-imagined the way frontend developers build fullstack applications. Develop and deploy without the hassle.

Page updated Apr 29, 2024

Fetch data

GET requests

To make a GET request, first build a RestOptions object and then use the Amplify.API.get api to issue the request:

void getTodo() {
RestOptions options = RestOptions.builder()
.addPath("/todo")
.build();
Amplify.API.get(options,
restResponse -> Log.i("MyAmplifyApp", "GET succeeded: " + restResponse),
apiFailure -> Log.e("MyAmplifyApp", "GET failed.", apiFailure)
);
}
private fun getTodo() {
val request = RestOptions.builder()
.addPath("/todo")
.build()
Amplify.API.get(request,
{ Log.i("MyAmplifyApp", "GET succeeded: $it") },
{ Log.e("MyAmplifyApp", "GET failed.", it) }
)
}
private suspend fun getTodo() {
val request = RestOptions.builder()
.addPath("/todo")
.build()
try {
val response = Amplify.API.get(request)
Log.i("MyAmplifyApp", "GET succeeded: ${response.data}")
} catch (error: ApiException) {
Log.e("MyAmplifyApp", "GET failed", error)
}
}
void getTodo() {
RestOptions options = RestOptions.builder()
.addPath("/todo")
.build();
RxAmplify.API.get(options)
.subscribe(
restResponse -> Log.i("MyAmplifyApp", "GET succeeded: " + restResponse),
apiFailure -> Log.e("MyAmplifyApp", "GET failed.", apiFailure)
);
}

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('/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:

RestOptions options = RestOptions.builder()
.addPath("/todo")
.addQueryParameters(Collections.singletonMap("q", "test"))
.build();
Amplify.API.get(options,
response -> Log.i("MyAmplifyApp", "GET succeeded: " + response),
error -> Log.e("MyAmplifyApp", "GET failed.", error)
);
val request = RestOptions.builder()
.addPath("/todo")
.addQueryParameters(mapOf("q" to "test"))
.build()
Amplify.API.get(request,
{ Log.i("MyAmplifyApp", "GET succeeded: $it") },
{ Log.e("MyAmplifyApp", "GET failed", it) }
)
val request = RestOptions.builder()
.addPath("/todo")
.addQueryParameters(mapOf("q" to "test"))
.build()
try {
val response = Amplify.API.get(request)
Log.i("MyAmplifyApp", "GET succeeded: $response.")
} catch (error: ApiException) {
Log.e("MyAmplifyApp", "GET failed", error)
}
RestOptions options = RestOptions.builder()
.addPath("/todo")
.addQueryParameters(Collections.singletonMap("q", "test"))
.build();
RxAmplify.API.get(options)
.subscribe(
response -> Log.i("MyAmplifyApp", "GET succeeded: " + response),
error -> Log.e("MyAmplifyApp", "GET failed.", error)
);