Search code examples
javascriptnode.jsamazon-web-servicesaws-lambdaalexa-skills-kit

AWS Lambda HTTP POST Request (Node.js)


I'm relatively new to AWS lambda function and nodejs. I'm working on to try and get the list of 5 cities in a country by using HTTP POST request from this website: "http://www.webservicex.net/globalweather.asmx?op=GetCitiesByCountry"

I've been searching about how to do a HTTP POST request in lambda function but I can't seem to find a good explanation for it.

Searches that I found for http post:

https://www.npmjs.com/package/http-post How to make an HTTP POST request in node.js?


Solution

  • Try the following sample, invoking HTTP GET or POST request in nodejs from AWS lambda

    const data = {
        "data": "your data"
    };
    const options = {
        hostname: 'hostname',
        port: port number,
        path: urlpath,
        method: 'method type'
    };
        
    const req = https.request(options, (res) => {
        res.setEncoding('utf8');
        res.on('data', (chunk) => {
        // code to execute
    });
    res.on('end', () => {
        // code to execute      
        });
    });
    req.on('error', (e) => {
         callback(null, "Error has occured");
    });
    req.write(data);
    req.end();
    

    Consider the sample