AWS Lambda - NodeJS POST request and asynch write/read file
13,147
In general, I'd recommend starting like this:
var querystring = require('querystring');
var https = require('https');
var fs = require('fs');
exports.handler = function(event, context) {
console.info('Received event', event);
var data = {
"accessKey": accessKey,
"templateName": templateName,
"outputName": outputName,
"data": event.data
};
// Build the post string from an object
var post_data = JSON.stringify(data);
// An object of options to indicate where to post to
var post_options = {
host: 'hostname.com',
port: '443',
path: '/path/to/api',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': post_data.length
}
};
var post_request = https.request(post_options, function(res) {
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
context.done(body);
});
res.on('error', function(e) {
context.fail('error:' + e.message);
});
});
// post the data
post_request.write(post_data);
post_request.end();
};
You can see I simplified your code quite a bit. I'd recommend avoiding the file system since that would slow down your program. I'm also not sure about what is the real goal of your function so I just return the HTTP response.
Related videos on Youtube
Author by
user5347553
Updated on June 24, 2022Comments
-
user5347553 almost 2 years
I am new to NodeJS and inside of AWS Lambda I am trying to make a POST request that calls an external API with a JSON object, creates a document with the response and then reads the contents of the file.
Coming from a Ruby background, I'm thinking the problem stems from my unfamiliarity with asynchronous programming, but I've tried using callbacks and readfileSync just to debug with no luck.
Any help would be appreciated.
var querystring = require('querystring'); var https = require('https'); var fs = require('fs'); exports.handler = function(event, context) { console.log('Received event:', JSON.stringify(event, null, 2)); var operation = event.operation; delete event.operation; var accessKey = event.accessKey; delete event.accessKey; var templateName = event.templateName; delete event.templateName; var outputName = event.outputName; delete event.outputName; var req = { "accessKey": accessKey, "templateName": templateName, "outputName": outputName, "data": event.data }; function doPost(data, callback) { // Build the post string from an object var post_data = JSON.stringify(data); // An object of options to indicate where to post to var post_options = { host: 'hostname.com', port: '443', path: '/path/to/api', method: 'POST', headers: { 'Content-Type': 'application/json', 'Content-Length': post_data.length } }; // Set up the request var file = fs.createWriteStream(outputName); var post_req = https.request(post_options, function(res) { res.setEncoding('utf8'); res.pipe(file); res.on('response', function(response) { console.log(response); }); res.on('error', function(e) { context.fail('error:' + e.message); }) res.on('end', function() { context.succeed('success, end request listener'); }); }); // post the data post_req.write(post_data); post_req.end(); callback(); } function printFileContents() { fs.readFileSync(outputName, 'utf8', function (err, data) { console.log('file contents:' + data); }); } switch (operation) { case 'create': // Make sure there's data before we post it if(req) { doPost(req, printFileContents); printFileContents(); } break; ... } };
-
Atlas Dostal about 5 years
var req
is not being used at all here, and accessKey, templateName, outputName arent defined anywhere. What are these for? -
adamkonrad about 5 yearsit was a mistake, the variable was supposed to be
data