dev1398
dev1398

Reputation: 1

HTTP request not getting data in AWS Lambda

When I use I paste this link in a browser or even an IDE in my local environment (Webstorm) I get JSON data but when I try to use this function in Lambda it returns an empty string (so the variable datastring is empty)

const https = require('https');

exports.handler = function(event,context,callback){

    let dataString = '';
    
    const req = https.get("https://www.instagram.com/dev1398/?__a=1", function(res) {
      res.on('data', chunk => {
        dataString += chunk;
      });
      res.on('end', () => {
        // console.log(JSON.parse(dataString));
        console.log(dataString)
      });
    });
    
    req.on('error', (e) => {
      console.error(e);
    });
}

Upvotes: 0

Views: 176

Answers (1)

Vipul Patil
Vipul Patil

Reputation: 1466

Use

const req = https.get("https://www.instagram.com/dev1398/?__a=1", function(res) {
       res.on('data', chunk => {
       dataString += chunk;
    });

in place of

const req = https.put("https://www.instagram.com/dev1398/?__a=1", function(res) {
    res.on('data', chunk => {
       dataString += chunk;
});

You need to use GET method instead of PUT

Upvotes: 1

Related Questions