# Access/get query parameters in aws lambda function

**URL:** <https://forum.serverless.com/t/access-get-query-parameters-in-aws-lambda-function/2141>\
**Category:** Serverless Framework\
**Created:** [June 25, 2017, 10:37pm UTC](https://forum.serverless.com/t/access-get-query-parameters-in-aws-lambda-function/2141 "2017-06-25T22:37:55Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![bhaskyknl](https://avatars.discourse-cdn.com/v4/letter/b/f19dbf/32.png) [@bhaskyknl](https://forum.serverless.com/u/bhaskyknl)\
**Post date:** [June 25, 2017, 10:37pm UTC](https://forum.serverless.com/t/access-get-query-parameters-in-aws-lambda-function/2141/1 "2017-06-25T22:37:55Z")

</div>

Hello everyone,

I am using eclipse. I am creating rest api using aws lambda java with serverless framework. I am having issue to understand how to define query parameters in serverless yml file and access them in the code, i am not able access them.

This is how i have defined my lambda function with query parameters in serverless.yml file.

functions:  
GetHistoryList:  
handler: com.services.handler.GetTHistoryListHandler  
events:  
- http:  
path: api/history/list  
method: GET  
integration: lambda  
private: true  
cors: true  
request:  
template:  
application/json: \>  
{  
“memberId” : “$input.params(‘memberId’)”,  
“fromDate” : “$input.params(‘fromDate’)”,  
“toDate” : “$input.params(’‘toDate’’)”  
}

I am trying access in my code like this:  
public class GetTHistoryListHandler extends RequestHandler\<Map\<String, Object\>, Response\> {

```
@Override
protected Response handleRequest(Map<String, Object> input, Context context) {
    String memberId= ((Map<String, String>) input.get("memberId")).get("memberId");
    String fromDate= ((Map<String, String>) input.get("fromDate")).get("fromDate");
    String toDate= ((Map<String, String>) input.get("toDate")).get("toDate");

```

}

My api url is like this:

GET - [https://host.com/dev/api/history/list?memberId=57692&fromDate=2017-06-16&toDate=2017-06-17](https://host.com/dev/api/history/list?memberId=57692&fromDate=2017-06-16&toDate=2017-06-17)

I deployed into aws lambda. I am not able access query params.

Please confirm me, what i have defined in yml and access them are correct or not.

Thanks,  
Bhasky

---

<div class="post-metadata">

**Author:** ![buggy](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/buggy/32/177_2.png) [@buggy](https://forum.serverless.com/u/buggy)\
**Post date:** [June 25, 2017, 11:56pm UTC](https://forum.serverless.com/t/access-get-query-parameters-in-aws-lambda-function/2141/2 "2017-06-25T23:56:12Z")

</div>

@bhaskyknl Indentation is important for YAML files. Can you please add ```yaml on the line before your serverless.yml and ``` on the line blow so that it maintains the correct formatting?

---

<div class="post-metadata">

**Author:** ![bhaskyknl](https://avatars.discourse-cdn.com/v4/letter/b/f19dbf/32.png) [@bhaskyknl](https://forum.serverless.com/u/bhaskyknl)\
**Post date:** [June 26, 2017, 2:01am UTC](https://forum.serverless.com/t/access-get-query-parameters-in-aws-lambda-function/2141/3 "2017-06-26T02:01:53Z")

</div>

```auto

functions:
GetHistoryList:
handler: com.services.handler.GetTHistoryListHandler
events:
- http:
path: api/history/list
method: GET
integration: lambda
private: true
cors: true
request:
template:
application/json: > 
{
"memberId" : "$input.params('memberId')",
"fromDate" : "$input.params('fromDate')",
"toDate" : "$input.params(''toDate'')"
}
```

---

<div class="post-metadata">

**Author:** ![bhaskyknl](https://avatars.discourse-cdn.com/v4/letter/b/f19dbf/32.png) [@bhaskyknl](https://forum.serverless.com/u/bhaskyknl)\
**Post date:** [June 26, 2017, 2:04am UTC](https://forum.serverless.com/t/access-get-query-parameters-in-aws-lambda-function/2141/4 "2017-06-26T02:04:19Z")

</div>

Thanks for replying…

you are saying like this?

```auto
functions:
GetHistoryList:
handler: com.services.handler.GetTHistoryListHandler
events:
- http:
path: api/history/list
method: GET
integration: lambda
private: true
cors: true
request:
template:
application/json: > 
{
"memberId" : "$input.params('memberId')",
"fromDate" : "$input.params('fromDate')",
"toDate" : "$input.params(''toDate'')"
}

```

---

<div class="post-metadata">

**Author:** ![buggy](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/buggy/32/177_2.png) [@buggy](https://forum.serverless.com/u/buggy)\
**Post date:** [June 26, 2017, 2:17am UTC](https://forum.serverless.com/t/access-get-query-parameters-in-aws-lambda-function/2141/5 "2017-06-26T02:17:45Z")

</div>

@bhaskyknl

If you do it correctly then your message will look like:

```auto
functions:
  GetHistoryList:
    handler: com.services.handler.GetTHistoryListHandler
    events:
      - http:
    path: api/history/list

```

Instead of

`
functions:
GetHistoryList:
handler: com.services.handler.GetTHistoryListHandler
events:
- http:
path: api/history/list
`

If you’re using code tags them remove them.

---

<div class="post-metadata">

**Author:** ![bhaskyknl](https://avatars.discourse-cdn.com/v4/letter/b/f19dbf/32.png) [@bhaskyknl](https://forum.serverless.com/u/bhaskyknl)\
**Post date:** [June 26, 2017, 2:23am UTC](https://forum.serverless.com/t/access-get-query-parameters-in-aws-lambda-function/2141/6 "2017-06-26T02:23:46Z")

</div>

I have correct indentation in my serverless.yml file, however i pasted here in forum, it is not formatting.

---

<div class="post-metadata">

**Author:** ![bhaskyknl](https://avatars.discourse-cdn.com/v4/letter/b/f19dbf/32.png) [@bhaskyknl](https://forum.serverless.com/u/bhaskyknl)\
**Post date:** [June 26, 2017, 6:27pm UTC](https://forum.serverless.com/t/access-get-query-parameters-in-aws-lambda-function/2141/7 "2017-06-26T18:27:19Z")

</div>

Is there any update on my issue?

---

<div class="post-metadata">

**Author:** ![bbilger](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/bbilger/32/1066_2.png) [@bbilger](https://forum.serverless.com/u/bbilger)\
**Post date:** [June 26, 2017, 10:35pm UTC](https://forum.serverless.com/t/access-get-query-parameters-in-aws-lambda-function/2141/8 "2017-06-26T22:35:15Z")

</div>

Hi @bhaskyknl,  
I think your mistake is that your template is incorrect since you have double single quotes around `toDate` (it must be `"toDate" : "$input.params('toDate')"` instead of `"toDate" : "$input.params(''toDate'')"`.

A few side notes:

1. `protected Response handleRequest` cannot be right. The visibility must be public!
2. Are you using the integration type `integration: lambda` on purpose? IMO `lambda-proxy` is much easier to use and gives you more flexibility. See [Docs](https://serverless.com/framework/docs/providers/aws/events/apigateway/) and [Java Template](https://github.com/serverless/serverless/blob/master/lib/plugins/create/templates/aws-java-gradle/src/main/java/com/serverless/Handler.java)
3. Since you are using Java, you might be interested in my Java framework [JRestless](https://github.com/bbilger/jrestless) allowing you to use JAX-RS on AWS-Lambda (for `lambda-proxy`, only).

_Full Example_

serverless.yml

```auto
service: history

provider:
  name: aws
  runtime: java8
  stage: dev
  region: eu-central-1

package:
  artifact: build/distributions/deployable.zip

functions:
  GetHistoryList:
    handler: com.services.handler.GetTHistoryListHandler
    events:
      - http:
          path: api/history/list
          method: GET
          integration: lambda
          request:
            template:
              application/json: >
                {
                  "memberId" : "$input.params('memberId')",
                  "fromDate" : "$input.params('fromDate')",
                  "toDate" : "$input.params('toDate')"
                }

```

```java
package com.services.handler;

import java.util.Map;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;

public class GetTHistoryListHandler implements RequestHandler<Map<String, Object>, GetTHistoryListHandler.Response>{

	@Override
	public GetTHistoryListHandler.Response handleRequest(Map<String, Object> input, Context context) {
		return new Response((String) input.get("memberId"), (String) input.get("fromDate"),
				(String) input.get("toDate"));
	}

    public static class Response {
    	private final String memberId;
    	private final String fromDate;
    	private final String toDate;
		Response(String memberId, String fromDate, String toDate) {
			super();
			this.memberId = memberId;
			this.fromDate = fromDate;
			this.toDate = toDate;
		}
		public String getMemberId() {
			return memberId;
		}
		public String getFromDate() {
			return fromDate;
		}
		public String getToDate() {
			return toDate;
		}
    }
}

```

build.gradle (just for completeness)

```auto
apply plugin: 'java'

repositories {
    mavenCentral()
}

dependencies {
    compile 'com.amazonaws:aws-lambda-java-core:1.1.0'
}

task buildZip(type: Zip) {
	archiveName = "deployable.zip"
	from compileJava
	from processResources
	into('lib') {
		from configurations.runtime
	}
}
build.dependsOn buildZip

```

---

<div class="post-metadata">

**Author:** ![bhaskyknl](https://avatars.discourse-cdn.com/v4/letter/b/f19dbf/32.png) [@bhaskyknl](https://forum.serverless.com/u/bhaskyknl)\
**Post date:** [June 28, 2017, 2:32am UTC](https://forum.serverless.com/t/access-get-query-parameters-in-aws-lambda-function/2141/9 "2017-06-28T02:32:38Z")

</div>

Thanks for the update. I am able to access query params now.

I went through the doc using lambda-proxy. Can you please provide if you have any example for HTTP POST method using java for lambda-proxy?

I am using spring boot. After i deployed into aws lambda, i am getting aws gateway timeout error for 1st run, subsequent runs gives result within 30 mins. Is there a solution for this timeout issue?

Thanks,  
Bhasky

---

<div class="post-metadata">

**Author:** ![bbilger](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/bbilger/32/1066_2.png) [@bbilger](https://forum.serverless.com/u/bbilger)\
**Post date:** [June 28, 2017, 9:06pm UTC](https://forum.serverless.com/t/access-get-query-parameters-in-aws-lambda-function/2141/10 "2017-06-28T21:06:28Z")

</div>

regarding binary  
check this: [How to add binary media type to serverless.yml](http://forum.serverless.com/t/how-to-add-binary-media-type-to-serverless-yml/2063/4?u=bbilger)  
The important steps are to

- to register binary media types (that’s not possible with serverless - you either need to do this manually or use some plugin)
- to send a “Content-Type” request-header with one of the registered binary media types as value
- decode the base64 encoded body, manually

regarding the timeout:

- Spring Boot is a probably a little heavy… try to unregister everything that’s not really required (add some logging to see where you actually waste the time)
- increase the timeout of your lambda function to 60s

---

<div class="post-metadata">

**Author:** ![bhaskyknl](https://avatars.discourse-cdn.com/v4/letter/b/f19dbf/32.png) [@bhaskyknl](https://forum.serverless.com/u/bhaskyknl)\
**Post date:** [June 29, 2017, 11:11pm UTC](https://forum.serverless.com/t/access-get-query-parameters-in-aws-lambda-function/2141/11 "2017-06-29T23:11:53Z")

</div>

Thanks for the update.

Can you please provide an example how we mention lambda-proxy / aws-proxy / aws\_proxy in serverless.yml and how we access the request/path variable using aws lambda java for PUT method?

Thanks,  
Bhasky

---

<div class="post-metadata">

**Author:** ![bbilger](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/bbilger/32/1066_2.png) [@bbilger](https://forum.serverless.com/u/bbilger)\
**Post date:** [July 3, 2017, 8:35pm UTC](https://forum.serverless.com/t/access-get-query-parameters-in-aws-lambda-function/2141/12 "2017-07-03T20:35:26Z")

</div>

Hi @bhaskyknl ,

1. You either remove `integration: lambda`, or replace it by `integration: lambda-proxy`.
2. You can get the resolved path from the “input map”: `(String) input.get("path")`
3. You can get the unresolved path from the “input map”: `(String) input.get("resource")`
4. You can get the path params from the “input map”: `(Map<String, String>) input.get("pathParams")`
5. You can get the query params from the “input map”: `(Map<String, String>) input.get("queryStringParameters")`

See: [http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-set-up-simple-proxy.html#api-gateway-simple-proxy-for-lambda-input-format](http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-set-up-simple-proxy.html#api-gateway-simple-proxy-for-lambda-input-format)
