1 個回答
- 最新
- 最多得票
- 最多評論
2
Hello.
I'm not sure what kind of code your Lambda function has, but if you keep the default code, I think you can make a request with the event JSON below.
Even if there is no need to include a body in the request, we specify an empty field by including ""data": {},".
{
"data": {},
"options": {
"hostname": "www.google.com"
}
}
The code below is generated by Lambda by default, but uses "data" in "req.write(JSON.stringify(event.data));".
import * as https from 'node:https';
/**
* Pass the data to send as `event.data`, and the request options as
* `event.options`. For more information see the HTTPS module documentation
* at https://nodejs.org/api/https.html.
*
* Will succeed with the response body.
*/
export const handler = (event, context, callback) => {
const req = https.request(event.options, (res) => {
let body = '';
console.log('Status:', res.statusCode);
console.log('Headers:', JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', (chunk) => body += chunk);
res.on('end', () => {
console.log('Successfully processed HTTPS response');
// If we know it's JSON, parse it
if (res.headers['content-type'] === 'application/json') {
body = JSON.parse(body);
}
callback(null, body);
});
});
req.on('error', callback);
req.write(JSON.stringify(event.data));
req.end();
};
相關內容
- AWS 官方已更新 2 年前
- AWS 官方已更新 3 年前
Thank you Riku! The data key was what was missing from my latest run at the code/test event. I thought I had seen another message of yours where you suggested a similar fix and given that a try. Obviously I didn't get it right before! :-)
For others following on, I started from scratch with a new function with the blueprint code and then used Riku's JSON and bingo!
If some AWS Lambda folks see this. Have you considered supplying working test event JSON as part of the blueprint so the blueprint is complete/functioning/starter code?
Oh the excitement! I have the test code calling into the API needed and a response is returned!
Thanks again!