You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

HTTP API调用AWS Lambda时参数返回undefined问题排查

Why is event.key1 undefined when calling my AWS Lambda via HTTP API (works in test console)?

I'm trying to pass a simple key1 parameter 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.key1 is undefined. My temporary API endpoint is https://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.queryStringParameters as suggested in some solutions, but it didn't work. Update: I can't figure out the correct way to access the parameter in Lambda—using event.body.key1 also 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:12:52