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

如何通过REST API调用向Lambda Event传递指定参数值

解决Lambda通过REST API接收参数的问题

一、先改Lambda代码:正确提取API传来的参数

你的Lambda现在没处理API Gateway传过来的event数据,导致找不到rows参数。根据请求类型,有两种常见的参数获取方式:

方式1:从POST请求的JSON体拿参数(推荐,适合rows这种数组类型)

修改lambda_handler,从请求体解析参数,同时加异常处理避免报错:

import json

def respond(err, res=None):
    return {
        'statusCode': '400' if err else '200',
        'body': err.message if err else json.dumps(res),
        'headers': {
            'Content-Type': 'application/json',
        },
    }

def lambda_handler(event, context):
    try:
        # 解析API Gateway的请求体,同时兼容直接Lambda测试的情况
        payload = json.loads(event['body']) if 'body' in event else event
        
        # 提取需要的5个参数
        rows = payload['rows']
        client = payload['client']
        insType = payload['insType']
        Source = payload['Source']
        email = payload['email']
        
        # 把你原来的CSV生成、S3上传逻辑放在这里
        # ...
        
        return respond(None, res="Success")
    except KeyError as e:
        return respond(Exception(f"缺必要参数: {str(e)}"))
    except Exception as e:
        return respond(Exception(f"执行出错: {str(e)}"))

方式2:从GET请求的查询参数拿参数(只适合简单参数,不建议传rows数组)

如果要用GET请求,从查询字符串里提取参数,注意数组要转成JSON字符串传递:

def lambda_handler(event, context):
    try:
        params = event.get('queryStringParameters', {})
        rows = json.loads(params['rows'])
        client = params['client']
        insType = params['insType']
        Source = params['Source']
        email = params['email']
        
        # 业务逻辑...
        
        return respond(None, res="Success")
    except KeyError as e:
        return respond(Exception(f"缺必要参数: {str(e)}"))
    except Exception as e:
        return respond(Exception(f"执行出错: {str(e)}"))

二、API Gateway配置:确保参数能传到Lambda

如果你用的是API Gateway创建的REST API,要注意:

  • 选Lambda Proxy integration(默认选项),这样API会把完整的请求信息(包括body、查询参数等)原样传给Lambda的event参数。
  • 别手动改映射模板,保持默认配置就行,不然容易丢参数。

三、测试API的方法

1. POST请求(用curl或Postman)

把参数放在JSON请求体里发送:

curl -X POST https://<你的API网关ID>.execute-api.<区域>.amazonaws.com/<阶段>/<资源路径> \
-H "Content-Type: application/json" \
-d '{
    "rows": [{"col1": "val1"}, {"col1": "val2"}],
    "client": "test-client",
    "insType": "typeA",
    "Source": "api",
    "email": "test@example.com"
}'

2. GET请求(仅简单场景用)

把参数拼在URL后面,注意rows要转成URL编码的JSON字符串:

curl "https://<你的API网关ID>.execute-api.<区域>.amazonaws.com/<阶段>/<资源路径>?rows=%5B%7B%22col1%22%3A%22val1%22%7D%2C%7B%22col1%22%3A%22val2%22%7D%5D&client=test-client&insType=typeA&Source=api&email=test%40example.com"

四、Lambda控制台测试的正确姿势

在Lambda控制台测试时,要模拟API Gateway的请求格式,测试事件写成:

{
  "body": "{\"rows\": [{\"col1\": \"val1\"}], \"client\": \"test\", \"insType\": \"typeA\", \"Source\": \"api\", \"email\": \"test@example.com\"}"
}

或者直接用原始的payload格式(代码里已经兼容这种情况):

{
  "rows": [{"col1": "val1"}],
  "client": "test",
  "insType": "typeA",
  "Source": "api",
  "email": "test@example.com"
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 12:18:26