Skip to content

Latest commit

 

History

History
38 lines (30 loc) · 1.03 KB

lambda-make-http-request.md

File metadata and controls

38 lines (30 loc) · 1.03 KB

How to make an HTTP request with AWS Lambda

In order to make an HTTP request with AWS Lambda and Node.js 14.x, you can use the default Node.js https module:

const https = require('https');

exports.handler = async (event) => {
    let dataString = '';
    
    const response = await new Promise((resolve, reject) => {
        const req = https.get("https://pokeapi.co/api/v2/pokemon/ditto", function(res) {
          res.on('data', chunk => {
            dataString += chunk;
          });

          res.on('end', () => {
            resolve({
                statusCode: 200,
                body: JSON.stringify(JSON.parse(dataString), null, 4)
            });
          });
        });
        
        req.on('error', (e) => {
          reject({
              statusCode: 500,
              body: 'Something went wrong!'
          });
        });
    });
    
    return response;
};

References: