I need my Amazon API Gateway REST API to pass query string parameters to a backend AWS Lambda function or an HTTP endpoint.
Short description
To configure a REST API to pass query string parameters to a backend AWS Lambda function, use a Lambda custom integration.
To pass query string parameters to an HTTP endpoint, use an HTTP custom integration.
Important: Make sure that you supply the input data as the integration request payload. It's a best practice to use a mapping template to supply the payload. For more information, see Parameter mapping examples for REST APIs in API Gateway.
Resolution
Pass query string parameters to a backend Lambda function
Complete the following steps:
- Open the API Gateway console, and then select your API.
- In the Resources pane, choose the configured HTTP method.
Note: If there's more than one HTTP method configured for the API, then repeat these steps for each method.
- In the Method Execution pane, choose Method Request and then choose Edit.
- Expand the URL Query String Parameters dropdown list, then choose Add query string.
- For the Name field, enter pet.
- Choose the Required check box, and then choose Save.
- Choose the Integration Request tab, and then choose Edit.
- Expand Mapping templates, and then choose Add mapping template.
- For the Content-Type field, enter application/json.
- In the mapping template editor, copy and replace the current script with the following code:
{ "pet": "$input.params('pet')"
}
Note: For more information, see the Input variables.
- Choose Save, and then choose Deploy the API.
- To test the API's new endpoint, run the following curl command:
curl -X GET https://jp58lnf5vh.execute-api.us-west-2.amazonaws.com/dev/lambda-non-proxy?pet=dog
Important: Make sure that the curl command has the query string parameter pet=dog.
Pass query string parameters to an HTTP endpoint
Complete the following steps:
- Open the API Gateway console, and then select your API.
- In the Resources pane, choose the configured HTTP method.
Note: If there's more than one HTTP method configured for the API, then repeat these steps for each method.
- In the Method Execution pane, choose Method Request, then choose Edit.
- Expand the URL Query String Parameters dropdown list, and then choose Add query string.
- For the Name field, enter type, and then choose Save.
- Choose the Integration Request tab, and then choose Edit.
- Expand the URL Query String Parameters section.
- For Name field, enter type.
- For the Mapped from field, enter method.request.querystring.type.
- Choose Save, then choose Deploy the API.
- To test the API's new endpoint, run the following curl command:
curl -X GET https://jp58lnf5vh.execute-api.us-west-2.amazonaws.com/dev/http-endpoint?pet=dog
Important: Make sure that the curl command has the query string parameter pet=dog.
Related information
Tutorial: Create a REST API with a Lambda proxy integration
Tutorial: Create a REST API with a Lambda non-proxy integration
Tutorial: Create a REST API with an HTTP proxy integration
Tutorial: Create a REST API with an HTTP non-proxy integration