How to access HTTP headers for request to AWS API Gateway using Lambda

Hi All,

I have created a simple lambda function in python as below

import json
def lambda_handler(event, context):  
    Body = {
        "response":{
           "resultStatus": "SUCCESS",
           "results":{
               "event": event
           }
        }
    }
    return {
        'statusCode': 200,
        'body': Body
    }

I have configured the same using API-gateway with default selection and didn’t make any changes in the gateway.

When I am hitting from soup-ui or postman using get-method getting a response only as

{
   "statusCode": 200,
   "body": {"response":    {
      "resultStatus": "SUCCESS",
      "results":       {
            "event": {}
      }
   }}
}

When trying with post-methods getting a response as

{
   "statusCode": 200,
   "body": {"response":    {
      "resultStatus": "SUCCESS",
      "results":       {
            "event": {
            "Number1": 10,
            "Number2": 15}
      }
   }}
}

Number1 & 2 are passed as the body of json request.

I am not able to get any header,queryParams,pathParams. Do I need to make any changes in the gateway?

Thanks!

That response looks wrong. My gut feeling is that you’ve configured the API Gateway to use Lambda integration but you’ve written the Lambda as though you’re using Lambda proxy integration. The API Gateway should be consuming the response and only returning the body in the HTTP body but here you’re saying it’s returning the entire response structure.

Hi @buggy , thanks for your reply . But I can’t get completely what you want to mean.

Do i need to change something?

Check your serverless.yml to see if you have the http event configured with integration: lambda. If you have the remove it and try again.

Hi @buggy , actually I did’t make with yml. I have first created lambda function in aws and then created API gateway pointing to that lambda function. I think here i can’t find serverless.yml file , please let me know if this can be extracted from API gateway .

If you’re not using the Serverless Framework then you need to look at the AWS console to figure out how you’ve configure the API gateway integration with Lambda. There are two methods Lambda and Lambda Proxy. My guess is you’re using Lambda and you need to be using Lambda Proxy. The other option is to keep Lambda and setting up mapping templates correctly.

Hi @buggy , Now I am able to complete this. Need to update mapping template in integration Request.