Rest-API 调用在 lambda 函数中失败
Rest-API call fails in lambda function
我对 AWS 中的 lambda 函数不熟悉,我需要一些建议来找出问题的本质。
AWS Lambda 函数基于 Javascript 使用 node.js 12.x。
我确实在 Ubuntu 18.04 和 MacOs Catalina 上设置了基于 SAM (sam cli/aws cli/docker/IntelliJ) 的本地开发环境,并且在两个系统上都运行了一个简单的基本 lambda 函数。
我可以设置日志并在 docker 运行时通过 IntelliJ 查看它们。
该函数是使用来自终端的 sam init 命令并选择一个简单的 hello world 创建的。
我确实在其中添加了一个 Rest-API 调用。
没什么特别的,使用请求 2.88.2(我知道它已被弃用,我确实尝试使用其他方法,但所有方法都失败了,所以我现在坚持请求)。
基本上,对 API 的调用“似乎”根本没有发生。
显示 API 调用前后的日志。
API 调用中的日志,比如显示错误或结果,永远不会显示。
到目前为止,只有在一种情况下,当我删除 URI 时,我才能看到来自 API 的错误消息。
正如预期的那样,API 返回了一条错误消息:无效的 URI。
否则什么都没有。这里有一些代码。
此函数从 lambda 处理程序调用。
function getToken() {
const request = require('request');
const qs = require('querystring');
console.log("getToken function called");
let bodyData = qs.stringify({
username: 'test',
password: 'xxxxx',
grant_type: 'password'
});
console.log("getToken bodyData : " + bodyData);
let options = {
url: "https://blahblahblah/function",
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
},
form: bodyData
};
console.log("getToken options : " + options);
var request1 = request(options, function(err, response, body) {
console.log("returned from API call");
if (err) {
console.log("Error from getToken : " + err);
return null;
} else {
console.log("Answer from getToken : " + body);
return body;
}
});
}
我确实使用 Postman 测试了连接和 API 并且正在运行。
请求中的所有日志都不会出现。
无论更改选项(确实尝试了许多不同的方法)。
我究竟做错了什么 ?
关于如何跟踪此问题的任何建议?
谢谢
史蒂夫
这是正确的行为。因为函数 getToken
并没有等待 http 请求完成。您应该将函数转换为使用 promise/async-await 或简单地回调。
承诺
async function getToken() {
...
return new Promise((resolve, reject) => {
request(options, function (err, response, body) {
console.log("returned from API call");
if (err) {
console.log("Error from getToken : " + err);
resolve(null);
} else {
console.log("Answer from getToken : " + body);
resolve(body);
}
});
});
}
// Then in the lambda Handler:
// await getToken()
回调
function getToken(callback) {
...
request(options, function (err, response, body) {
console.log("returned from API call");
if (err) {
console.log("Error from getToken : " + err);
callback(null);
} else {
console.log("Answer from getToken : " + body);
callback(body);
}
});
}
// Then in the lambda
getToken(() => {
// handle the http request response
})
我对 AWS 中的 lambda 函数不熟悉,我需要一些建议来找出问题的本质。 AWS Lambda 函数基于 Javascript 使用 node.js 12.x。 我确实在 Ubuntu 18.04 和 MacOs Catalina 上设置了基于 SAM (sam cli/aws cli/docker/IntelliJ) 的本地开发环境,并且在两个系统上都运行了一个简单的基本 lambda 函数。 我可以设置日志并在 docker 运行时通过 IntelliJ 查看它们。 该函数是使用来自终端的 sam init 命令并选择一个简单的 hello world 创建的。
我确实在其中添加了一个 Rest-API 调用。 没什么特别的,使用请求 2.88.2(我知道它已被弃用,我确实尝试使用其他方法,但所有方法都失败了,所以我现在坚持请求)。
基本上,对 API 的调用“似乎”根本没有发生。 显示 API 调用前后的日志。 API 调用中的日志,比如显示错误或结果,永远不会显示。 到目前为止,只有在一种情况下,当我删除 URI 时,我才能看到来自 API 的错误消息。 正如预期的那样,API 返回了一条错误消息:无效的 URI。
否则什么都没有。这里有一些代码。 此函数从 lambda 处理程序调用。
function getToken() {
const request = require('request');
const qs = require('querystring');
console.log("getToken function called");
let bodyData = qs.stringify({
username: 'test',
password: 'xxxxx',
grant_type: 'password'
});
console.log("getToken bodyData : " + bodyData);
let options = {
url: "https://blahblahblah/function",
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
},
form: bodyData
};
console.log("getToken options : " + options);
var request1 = request(options, function(err, response, body) {
console.log("returned from API call");
if (err) {
console.log("Error from getToken : " + err);
return null;
} else {
console.log("Answer from getToken : " + body);
return body;
}
});
}
我确实使用 Postman 测试了连接和 API 并且正在运行。 请求中的所有日志都不会出现。 无论更改选项(确实尝试了许多不同的方法)。 我究竟做错了什么 ? 关于如何跟踪此问题的任何建议?
谢谢
史蒂夫
这是正确的行为。因为函数 getToken
并没有等待 http 请求完成。您应该将函数转换为使用 promise/async-await 或简单地回调。
承诺
async function getToken() {
...
return new Promise((resolve, reject) => {
request(options, function (err, response, body) {
console.log("returned from API call");
if (err) {
console.log("Error from getToken : " + err);
resolve(null);
} else {
console.log("Answer from getToken : " + body);
resolve(body);
}
});
});
}
// Then in the lambda Handler:
// await getToken()
回调
function getToken(callback) {
...
request(options, function (err, response, body) {
console.log("returned from API call");
if (err) {
console.log("Error from getToken : " + err);
callback(null);
} else {
console.log("Answer from getToken : " + body);
callback(body);
}
});
}
// Then in the lambda
getToken(() => {
// handle the http request response
})