HTTP API调用AWS Lambda时参数返回undefined问题排查
event.key1 undefined when calling my AWS Lambda via HTTP API (works in test console)? I'm trying to pass a simple
key1parameter to my AWS Lambda function via HTTP API. When I test the Lambda with{"key1": "value1"}in the AWS test console, it works fine and returns the correct value. But when I call the associated HTTP API using Postman,event.key1is undefined. My temporary API endpoint ishttps://5fitm6ca95.execute-api.us-east-1.amazonaws.com/default/getPresignedURLforEmailAttachments.Here's my Lambda code:
exports.handler = async (event) => { // TODO implement const response = { statusCode: 200, body: "They key is " + event.key1 + ". Event console log: " + JSON.stringify(event), }; return response; };I've tried using
event.queryStringParametersas suggested in some solutions, but it didn't work. Update: I can't figure out the correct way to access the parameter in Lambda—usingevent.body.key1also returns undefined.
Let's break down the fix based on how you're sending the request to your HTTP API:
1. If you're using a GET request (parameters in the URL)
When you send a GET request with key1 in the query string (like https://5fitm6ca95.execute-api.us-east-1.amazonaws.com/default/getPresignedURLforEmailAttachments?key1=value1), the parameter lives in event.queryStringParameters. You need to access it like this:
exports.handler = async (event) => { // Use optional chaining to avoid errors if no query params are sent const key1 = event.queryStringParameters?.key1; const response = { statusCode: 200, body: `The key is ${key1}. Event console log: ${JSON.stringify(event)}`, }; return response; };
If this didn't work before, double-check your Postman setup: make sure you're using a GET request, and the key1 parameter is added to the Params tab (not the request body).
2. If you're using a POST request (JSON body)
When you send a POST with a JSON body like {"key1": "value1"}, API Gateway sends the raw JSON string in event.body—you can't access event.body.key1 directly because it's a string, not an object. You need to parse it first:
exports.handler = async (event) => { let key1; // Only parse if there's a body present if (event.body) { const parsedBody = JSON.parse(event.body); key1 = parsedBody.key1; } const response = { statusCode: 200, body: `The key is ${key1}. Event console log: ${JSON.stringify(event)}`, }; return response; };
Critical note for Postman: Set your request body to Raw > JSON, and ensure the Content-Type header is set to application/json. If you skip this, API Gateway won't recognize the body as valid JSON, and parsing will fail.
Debugging tip: Check the full event structure
The JSON.stringify(event) in your response already shows you the complete event format. Look for fields like queryStringParameters, body, or pathParameters—this will tell you exactly where your parameter is being stored. You can also check CloudWatch Logs for your Lambda to see the full event object if the response doesn't give you enough info.
内容的提问来源于stack exchange,提问作者Smlok

