问题
exports.handler = async (event) => {
// TODO implement
const https = require('https');
https.get('https://postman-echo.com/get?' +
'username =' +
'&password =' +
'&date=' +
'&cashSales=' + +
'&creditCardVisa=' +
'&creditCardMaster=' + +
'&creditCardAmex=' +
'&creditCardOthers=0',
res => {
//console.log(res.statusCode);
//console.log(res.headers);
let body = '';
res.on('data', data => {
body += data;
})
res.on('end', () => console.log(body));
})
const response = {
statusCode: 200,
body: JSON.stringify(https.get),
};
return response;
};
I can't seem to output the http request using this function, this can run in node.js but not in aws lambda, even after putting it in response function.
回答1:
I have restructed the code a bit and wrapped it in a promise. You were not returning anything from the function. see here
const https = require("https");
exports.handler = event => {
return Promise((resolve, reject) => {
https.get(
"https://postman-echo.com/get?" +
"username =" +
"&password =" +
"&date=" +
"&cashSales=" +
+"&creditCardVisa=" +
"&creditCardMaster=" +
+"&creditCardAmex=" +
"&creditCardOthers=0",
resp => {
let data = "";
// A chunk of data has been recieved.
resp.on("data", chunk => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on("end", () => {
resolve(data);
});
resp.on("error", error => {
reject(error);
});
}
);
});
};
回答2:
you were close. But I found two issues.
- Your lambda function was not waiting for the http get to finish.
- You were returning the incorrect value
https.get
. you should return the response payload of thehttps.get
call.
I have fixed the issues above.
const https = require("https");
exports.handler = async (event) => {
const httpResponse = await new Promise((resolve, reject) => {
https.get(
"https://postman-echo.com/get?" +
"username =" +
"&password =" +
"&date=" +
"&cashSales=" +
+"&creditCardVisa=" +
"&creditCardMaster=" +
+"&creditCardAmex=" +
"&creditCardOthers=0",
resp => {
let body = '';
// A chunk of data has been recieved.
resp.on("data", chunk => {
body += chunk;
});
// The whole response has been received. Print out the result.
resp.on("end", () => {
resolve(body);
});
resp.on("error", error => {
reject(error);
});
}
);
});
const response = {
statusCode: 200,
body: JSON.stringify(httpResponse),
};
return response;
};
hope this helps.
回答3:
I think others have already pointed out the problem in your code. You are not waiting for the http get call to complete before returning. Read this article which explains the incorrect handling of Promises in AWS Lambda functions and the solutions for that.
If you use request-promise library (a much more popular one on npm) instead of https and with the new async/await syntax, your code will become very simple.
exports.handler = async function(event) {
const request = require('request-promise');
const res = await request.get('https://postman-echo.com/get', {
params: {
username: '',
password: '',
date: '',
cashSales: '',
creditCardVisa: '',
creditCardMaster: '',
creditCardAmex: '',
creditCardOthers: '0'
}
});
const response = {
statusCode: 200,
body: JSON.stringify(res)
};
return response;
};
来源:https://stackoverflow.com/questions/59518609/output-http-request-node-js-in-aws-lambda