## GET requests

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

<BlockSwitcher>
<Block name="Java">

```java
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)
    );
}
```

</Block>
<Block name="Kotlin - Callbacks">

```kotlin
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) }
    )
}
```

</Block>
<Block name="Kotlin - Coroutines">

```kotlin
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)
    }
}
```

</Block>
<Block name="RxJava">

```java
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)
        );
}
```

</Block>
</BlockSwitcher>

## Accessing query parameters & body in Lambda proxy function

> To learn more about Lambda Proxy Integration, please visit [Amazon API Gateway Developer Guide](https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html).

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:

```javascript
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:

```javascript
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`:

```javascript
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:

<BlockSwitcher>
<Block name="Java">

```java
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)
);
```

</Block>
<Block name="Kotlin - Callbacks">

```kotlin
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) }
)
```

</Block>
<Block name="Kotlin - Coroutines">

```kotlin
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)
}
```

</Block>
<Block name="RxJava">

```java
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)
    );
```

</Block>
</BlockSwitcher>
