Skip to content

API Gateway / Lambda - Receive Only Request Body Without Full Event

0

Hello everyone,

I have an API Gateway that calls a Lambda function via a proxy integration.

I have two endpoints configured the same way:

First endpoint (works correctly)

POST https://5ow33fr5xh.execute-api.us-east-1.amazonaws.com/get-catalog-by-id/categories/catalog In CloudWatch logs, my Lambda directly receives the JSON object I sent:

{
  "categoryId": "390"
}

Second endpoint (problematic)

POST https://5ow33fr5xh.execute-api.us-east-1.amazonaws.com/get-external-catalog/categories/external_category In CloudWatch logs, my Lambda receives the full API Gateway event, meaning I have to do JSON.parse(event.body), example:

{
  "resource": "/categories/external_category",
  "path": "/categories/external_category",
  "httpMethod": "POST",
  "headers": { ... },
  "body": "{\r\n    \"categoryId\": \"390\"\r\n}\r\n",
  "isBase64Encoded": false
}

What I Want I would like my second Lambda to receive the raw JSON body directly, without having to parse event.body.

What I Have Checked Proxy integration is enabled for both endpoints. The "Request body passthrough" option in API Gateway is set to "When no template matches the request Content-Type (recommended)". I tried adding mapping templates, but it didn’t change anything. IAM roles and permissions are the same for both Lambdas. Question How can I configure API Gateway so my Lambda directly receives the JSON body (instead of the full API Gateway event)?

Thanks for your help! 😊

1 Answer
1

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:

  1. 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.

  2. 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:

  1. 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.

  2. 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.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.#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)
answered a year ago
EXPERT
reviewed a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.