- Newest
- Most votes
- Most comments
The behavior you're experiencing with your second endpoint is actually the standard behavior for Lambda proxy integrations with API Gateway. In a proxy integration, Lambda receives the full API Gateway event, including headers, query parameters, and the request body.
The fact that your first endpoint is receiving only the JSON body directly is unusual and may be due to some custom configuration or processing that's not immediately apparent.
To achieve what you want for your second endpoint, you have a few options:
-
Keep the proxy integration and parse the body in your Lambda function: This is the most straightforward approach. You can simply use JSON.parse(event.body) in your Lambda to get the JSON object.
-
Use a non-proxy integration: You can switch to a non-proxy integration and use a mapping template to pass only the body to your Lambda. In the Integration Request, you would set up a mapping template like this:
#set($inputRoot = $input.path('The behavior you're experiencing with your second endpoint is actually the standard behavior for Lambda proxy integrations with API Gateway. In a proxy integration, Lambda receives the full API Gateway event, including headers, query parameters, and the request body.
The fact that your first endpoint is receiving only the JSON body directly is unusual and may be due to some custom configuration or processing that's not immediately apparent.
To achieve what you want for your second endpoint, you have a few options:
-
Keep the proxy integration and parse the body in your Lambda function: This is the most straightforward approach. You can simply use JSON.parse(event.body) in your Lambda to get the JSON object.
-
Use a non-proxy integration: You can switch to a non-proxy integration and use a mapping template to pass only the body to your Lambda. In the Integration Request, you would set up a mapping template like this:
#set($inputRoot = $input.path('$')) $inputRoot.bodyThis will pass only the body of the request to your Lambda.
-
Use a custom authorizer or middleware: If you want to keep the proxy integration but avoid parsing in your main Lambda function, you could use a custom authorizer or a middleware Lambda that processes the event and passes only the body to your main function.
-
Use API Gateway's request body passthrough: You mentioned you've already tried this, but double-check that the "When no template matches the request Content-Type" option is selected and that you don't have any mapping templates defined for the specific Content-Type of your request.
Remember, while it's possible to configure API Gateway to pass only the body, the full event can be useful for accessing other request information like headers or query parameters. If you only need the body in most cases but occasionally need other parts of the event, you might consider keeping the full event and creating a helper function to extract the body when needed.#39;)) $inputRoot.body
This will pass only the body of the request to your Lambda.
3. Use a custom authorizer or middleware:
If you want to keep the proxy integration but avoid parsing in your main Lambda function, you could use a custom authorizer or a middleware Lambda that processes the event and passes only the body to your main function.
4. Use API Gateway's request body passthrough:
You mentioned you've already tried this, but double-check that the "When no template matches the request Content-Type" option is selected and that you don't have any mapping templates defined for the specific Content-Type of your request.
Remember, while it's possible to configure API Gateway to pass only the body, the full event can be useful for accessing other request information like headers or query parameters. If you only need the body in most cases but occasionally need other parts of the event, you might consider keeping the full event and creating a helper function to extract the body when needed.
**Sources**
[Requests to API Gateway fail with CORS error if I add headers. | AWS re:Post](https://repost.aws/questions/QUEXhLmmw6Rs-I819fPue1LQ/requests-to-api-gateway-fail-with-cors-error-if-i-add-headers)
[Set up asynchronous invocation of the backend Lambda function - Amazon API Gateway](https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-integration-async.html)
Relevant content
- asked 2 years ago
