如何通过AWS HTTP API将参数发送到Lambda

问题描述

我已经从REST API切换到HTTP API,因为它具有成本效益。我是AWS的菜鸟,所以请耐心等待。

LAMBDA

我有一个简单的Lambda函数,该函数返回给定的相同值。从实际的Lambda控制台进行测试后,它可以正常工作。

exports.handler = async (event) => {
    // TODO implement
    const response = {
        statusCode: 200,body: "hello" + event["key1"]
    };
    return response;
};

从Lambda控制台使用JSON输入测试功能时: {“ key1”:“ value1”},它返回“ hellovalue1”。 这是我希望我的GET / POST请求返回的内容。

API网关(HTTP API)

我创建了一个简单的HTTP API,该API导致了上述的lambda函数。 https://39lzilxqm2.execute-api.eu-central-1.amazonaws.com/MS_APITest

从浏览器调用上面的链接时,它返回 helloundefined

我认为传递参数的方式是[THE LINK ABOVE]?key1 = value1,但这也会返回 helloundefined

使用其他在线工具通过GET / POST请求将JSON数据发送到上面的链接时,结果再次是 helloundefined

荣誉奖:通过邮递员发送请求时,它会显示错误:

CORS错误:由于CORS政策,请求已被阻止

如何使用HTTP API将参数传递给AWS Lambda?

谢谢。

解决方法

我尝试使用HTTP API 复制问题。我注意到该活动具有以下形式。

{
  version: '2.0',routeKey: 'ANY /MS_APITest',rawPath: '/MS_APITest',rawQueryString: 'key1=value1',headers: {
    accept: '*/*','content-length': '0',host: 'xxxxx.execute-api.us-east-1.amazonaws.com','user-agent': 'curl/7.72.0','x-amzn-trace-id': 'Root=1-5f5afd55-332693f425fbcc7a032809da','x-forwarded-for': 'xxxxxxxxx','x-forwarded-port': '443','x-forwarded-proto': 'https'
  },queryStringParameters: { key1: 'value1' },requestContext: {
    accountId: '820872329501',apiId: 'sg5mhha5ic',domainName: 'xxxx.execute-api.us-east-1.amazonaws.com',domainPrefix: 'sg5mhha5ic',http: {
      method: 'GET',path: '/MS_APITest',protocol: 'HTTP/1.1',sourceIp: 'xxxxxx',userAgent: 'curl/7.72.0'
    },requestId: 'SryFYjtUIAMEV5w=',stage: '$default',time: '11/Sep/2020:04:30:13 +0000',timeEpoch: 1599798613551
  },isBase64Encoded: false
}

可以看出,?key1=value1的传递方式为

queryStringParameters: { key1: 'value1' },

因此,lambda函数应为:

exports.handler = async (event) => {
    // TODO implement
    console.log(event)
    const response = {
        statusCode: 200,body: "hello" + event['queryStringParameters']["key1"]
    };
    return response;
};

验证,它可以使用:

curl https://xxxx.execute-api.us-east-1.amazonaws.com/MS_APITest?key1=value1

导致:

hellovalue1
,

要由您的api网关配置正确地将某些内容(查询参数,POST或PUT正文(请注意,忽略GET请求正文),表单数据)映射到Lambda的Json输入。仔细观察https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html-注意如何

整个请求通过代表任何HTTP方法的全部ANY方法直接发送到后端Lambda函数。客户端在运行时指定实际的HTTP方法。 ANY方法允许您对所有受支持的HTTP方法使用单个API方法设置:DELETE,GET,HEAD,OPTIONS,PATCH,POST和PUT。

后来他们展示了如何

curl -v -X POST "https://r275xc9bmd.execute-api.us-east-1.amazonaws.com/test/helloworld?name=John&city=Seattle" -H "content-type: application/json" -H "day: Thursday" -d "{ \"time\": \"evening\" }"

或者,对于GET请求,

curl -X GET \
  'https://r275xc9bmd.execute-api.us-east-1.amazonaws.com/test/helloworld?name=John&city=Seattle' \
  -H 'content-type: application/json' \
  -H 'day: Thursday'

全部捕获在此处提供的全部配置中。

但是,如果集成不太正确,则数据将无法正确传递,因此请仔细观察。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...