Im trying to send a simple GET request to a URL and get the response back. I found below code that works but it only display Success but I would like to see the response. Something like this using curl:
curl -X GET http://www.some.url.com
const http = require('http');
exports.handler = async (event, context) => {
return new Promise((resolve, reject) => {
const options = {
host: 'jsonplaceholder.typicode.com',
path: '/posts/1/',
port: 80,
method: 'GET'
};
const req = http.request(options, (res) => {
resolve('Success');
});
req.on('error', (e) => {
reject(e.message);
});
// send the request
req.write('');
req.end();
});
};
For your Node.JS application, AWS Cloudwatch log the console information for Lambda function. You can use console.log() with a sample request like so
const http = require('http');
exports.handler = async (event, context) => {
return new Promise((resolve, reject) => {
const options = {
host: 'jsonplaceholder.typicode.com',
path: '/posts/1/',
port: 80,
method: 'GET'
};
const req = http.request(options, (res) => {
let data = '';
// A chunk of data has been recieved.
res.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Print out the result.
res.on('end', () => {
console.log(JSON.parse(data));
});
});
req.write('');
req.end();
});
};
The output would show, for example:
Function Logs:
START RequestId: ... Version: $LATEST
2019-05-08T10:35:11.650Z ... { userId: 1,
id: 1,
title: 'sunt aut facere repellat provident occaecati excepturi optio reprehenderit',
body: 'quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto' }
END RequestId: ...