9

I have uploaded an AWS Lambda function where the lambda_handler looks as follows:

import json def lambda_handler(event, context): print(event) return { 'statusCode': 200, 'body': json.dumps('Hello from Lambda!'), 'event': event } 

Problem 1: returning event

When I test it using the Lambda Management Console I can create a test event with parameters which also return the exact same format and all works fine:

{ "key1": "value1", "key2": "value2", "key3": "value3" } 

However, when I use Postman, then I get something totally else, which returns to me:

{ "message": "Internal server error" } 

I suspect its because the event looks something more like:

{'resource': '/hello', 'path': '/hello', 'httpMethod': 'GET', 'headers': {'Accept': '*/*', ... etc 

Problem 2: adding json parameters in body creates an error

When I try in Postman to add in the body > raw > JSON(application/JSON) the keys above, then I get the error:

ERROR: The request could not be satisfied 

Questions

I have two questions:

  • How do I pass parameters in the body and be able to capture it in AWS lambda using the event or context?
  • How do I return the event properly?
5
  • Are you passing the header with Content-Type:application/json? Commented Jan 10, 2019 at 12:16
  • In body I specify that right? Commented Jan 10, 2019 at 12:17
  • Not in the body, but as a header. Test this on Postman first. Also, you must be using an API link for this? Commented Jan 10, 2019 at 12:18
  • The API link may require some sort of authentication as well. Please share the status code that you get on Postman. For internal server error, its most likely 500. Commented Jan 10, 2019 at 12:27
  • 1
    When you are sending information in the body, you should do a POST request. If you are trying to do a GET request, the body has no significance. This link maybe helpful docs.aws.amazon.com/apigateway/latest/developerguide/… Commented Jan 10, 2019 at 12:31

2 Answers 2

10

Assuming you have set up your Lambda as proxy integration in AWS API Gateway. If you want to attach query string params and no body then your method type should be GET.

The event you would receive in your Lambda for request /GET your-path/?myKey1=value1&myKey2=value2 should something like:

{ "resource": "", "path": "/your-path", "httpMethod": "GET", "headers": { }, "queryStringParameters": { "myKey1": "value1", "myKey2": "value2" }, "pathParameters": { }, "body": "{}" } 

You can access query string params in queryStringParameters property.

If you send request using Postman and attach body then your Lambda integration type should be POST/PUT. Data that you add in Postman request body would be available in event["body"].

Last thing if you test Lambda directly in the console then event will be received as you put in the body. You will need to format your event according to the integration method type. If it is POST/PUT then:

{ "body": { "someValue": {..} } } 

If it is GET then:

{ "queryStringParameters": { "myKey1": "value1", "myKey2": "value2" } } 
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. Tried that, didnt work, again internal server error from the Postman
Cloudwatch logs show that all went fine, no errors... So I guess it something with returning the event.
Can you confirm Python code you attached in the original post is the one you are still using? If so you need to remove event property from the response. Can you confirm you've set up proxy integration in gateway? Also please confirm integration method type and request you are posting to endpoint
No proxy integration. But I find it weird that on the logs I do not see "body" in the event, just headers. How is that possible?
2

Figrued it out, after help from @Althar Khan.

Apparently the API Gateway of AWS Lambda only accepts certain properties:

... return { "statusCode": 200, "headers": { "my_header": "my_value" }, "body": JSON.stringify(responseBody), "isBase64Encoded": false }; 

In this response, there are four fields: statusCode, headers, body, and isBase64Encoded.

In this example, the function's response is in the format that the API Gateway expects. For additional information, see Output Format of a Lambda Function for Proxy Integration.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.