如何通过AWS HTTP API将参数发送到Lambda [英] How to send parameters through AWS HTTP API to Lambda
问题描述
我已经从REST API切换到HTTP API,因为它具有成本效益.我是AWS的菜鸟,所以请耐心等待我.
I have switched from the REST API to HTTP API for it's cost efficiency. I am a noob to AWS so please be patient with me.
LAMBDA
我有一个简单的Lambda函数,它返回的值与给定的值相同.从实际的Lambda控制台进行测试后,它可以很好地工作.
I have a simple Lambda function that returns the same value it's given. It works fine when tested from the actual Lambda console.
exports.handler = async (event) => {
// TODO implement
const response = {
statusCode: 200,
body: "hello" + event["key1"]
};
return response;
};
从Lambda控制台使用JSON输入测试功能时:{"key1":"value1"},则返回"hellovalue1".这是我希望我的GET/POST请求返回的内容.
When the function is tested from the Lambda console with JSON input: { "key1":"value1" }, it returns "hellovalue1". This is what I want my GET/POST request to return.
API网关(HTTP API)
我创建了一个简单的HTTP API,该API导致了上述的lambda函数. https://39lzilxqm2.execute-api.eu-central-1.amazonaws.com/MS_APITest
I created a simple HTTP API which leads to the lambda function above. https://39lzilxqm2.execute-api.eu-central-1.amazonaws.com/MS_APITest
从浏览器调用上面的链接时,它返回 helloundefined .
When the link above is called from the browser, it returns helloundefined.
我假设传递参数的方式是[THE LINK ABOVE]?key1 = value1,但这也会返回 helloundefined
I would assume the way of passing an argument would be [THE LINK ABOVE]?key1=value1, but that also returns helloundefined
当使用其他在线工具通过GET/POST请求将JSON数据发送到上面的链接时,结果再次是 helloundefined
When using other online tools to send JSON data to the link above through GET/POST requests, the result is, again, helloundefined
荣誉奖:通过邮递员发送请求时,它会显示错误:
Honorable mention: When sending a request through postman, it displays an error:
CORS错误:由于CORS政策,请求已被阻止
CORS Error: The request has been blocked because of the CORS policy
如何使用HTTP API将参数传递给AWS Lambda?
How do I pass an argument to AWS Lambda using HTTP API?
谢谢.
推荐答案
我尝试使用HTTP API 复制该问题.我注意到该活动具有以下形式.
I tried to replicate the issue using HTTP API. I noticed that the event has the following form.
{
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=',
routeKey: 'ANY /MS_APITest',
stage: '$default',
time: '11/Sep/2020:04:30:13 +0000',
timeEpoch: 1599798613551
},
isBase64Encoded: false
}
可以看出,?key1 = value1
的传递方式为
As can be seen the ?key1=value1
is passed as
queryStringParameters: { key1: 'value1' },
因此,lambda函数应为:
Therefore, the lambda function should be:
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
这篇关于如何通过AWS HTTP API将参数发送到Lambda的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!