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

如何从API Gateway测试界面正确传递参数至Lambda函数

问题解决:API Gateway查询字符串无法传递到Lambda函数

问题分析

核心原因是API Gateway未配置请求映射模板——默认情况下,它不会自动把查询字符串、路径参数等请求数据传递给Lambda,所以Lambda接收的event是空字典。

解决步骤

1. 配置API Gateway集成请求的映射模板

  • 打开API Gateway控制台,找到目标API和对应的方法(比如你创建的GET方法)
  • 切换到「集成请求」标签页
  • 滚动到「映射模板」区域,点击「添加映射模板」,输入application/json并确认
  • 勾选「当没有模板匹配请求内容类型时通过」选项
  • 在模板编辑框中粘贴以下JSON内容,将查询字符串映射到Lambda的event对象:
    {
      "queryStringParameters": "$input.params().querystring"
    }
    
  • 点击「保存」,重新部署API(配置变更必须部署才会生效)

2. 验证参数传递(可选)

修改Lambda代码,明确读取查询参数:

def lambda_handler(event, context):
    print("完整event:", event)
    # 读取查询字符串参数
    query_params = event.get('queryStringParameters', {})
    print("接收到的查询参数:", query_params)
    return {
        "statusCode": 200,
        "body": query_params
    }

补充说明

配置映射模板后,API Gateway会把请求中的查询字符串打包到event的queryStringParameters字段中。如果需要传递其他请求数据(比如请求体、路径参数),可以在映射模板中添加对应字段,示例:

{
  "queryStringParameters": "$input.params().querystring",
  "pathParameters": "$input.params().path",
  "body": "$input.json('$')"
}

原始信息

Lambda代码

def lambda_handler(event, context):
    print("hello")
    print("event:", event)
    print("hello")
    return event

Lambda执行日志

INIT_START Runtime Version: python:3.11.v14     Runtime Version ARN: arn:aws:lambda:us-west-2::runtime:9c87c21a94b293e1a306aad2c23cfa6928e7a79a3d3356158b15f4cbe880b390
        START RequestId: 459979a0-f0f5-4571-9198-a5bb57ff1943 Version: $LATEST
        hello
        event: {}
        hello
        END RequestId: 459979a0-f0f5-4571-9198-a5bb57ff1943
        REPORT RequestId: 459979a0-f0f5-4571-9198-a5bb57ff1943  Duration: 1.72 ms       Billed Duration: 2 ms   Memory Size: 128 MB     Max Memory Used: 72 MB  Init Duration: 316.97 ms    

API配置界面

API配置界面

API测试界面

API测试界面


内容的提问来源于stack exchange,提问作者Joymaker

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 14:20:10