How to call a POST API from lambda function?

a 夏天 提交于 2021-02-11 12:25:30

问题


I am trying to integrate a POST API call from a lambda function using Node.js 12.x.

I tried like below:

var posturl = "My post api path";
var jsonData = "{'password':'abcdef','domain':'www.mydomain.com','username':'abc.def'}";
var req = require('request');
const params = {
    url: posturl,
    headers: { 'jsonData': jsonData }
};
req.post(params, function(err, res, body) {
    if(err){
        console.log('------error------', err);
    } else{
        console.log('------success--------', body);
    }
});

But when I am execute it using state machine, I am getting the below exception:

{
  "errorType": "Error",
  "errorMessage": "Cannot find module 'request'\nRequire stack:\n- /var/task/index.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
  "trace": [
    "Error: Cannot find module 'request'",
    "Require stack:",
    "- /var/task/index.js",
    "- /var/runtime/UserFunction.js",
    "- /var/runtime/index.js",
    "    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:815:15)",
    "    at Function.Module._load (internal/modules/cjs/loader.js:667:27)",
    "    at Module.require (internal/modules/cjs/loader.js:887:19)",
    "    at require (internal/modules/cjs/helpers.js:74:18)",
    "    at Runtime.exports.handler (/var/task/index.js:8:14)",
    "    at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"
  ]
}

Here the posturl is my api path and jsondata is my key value pair data.

So How can I call a POST API from lambda function? How can I pass the entire jsondata key when call API? How can I parse the response after the service call?

Update: I have tried like below

All my details are passing with a key jsonData, where I can specify that? Without that key, it will not work.

exports.handler = (event, context, callback) => {
    const http = require('http');
    const data = JSON.stringify({
    password: 'mypassword',
    domain: 'www.mydomain.com',
    username: 'myusername'
});

const options = {
    hostname: 'http://abc.mydomain.com',
    path: 'remaining path with ticket',
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        'Content-Length': data.length
    }
};

const req = http.request(options, (res) => {
    let data = '';

    console.log('Status Code:', res.statusCode);

    res.on('data', (chunk) => {
        data += chunk;
    });

    res.on('end', () => {
        console.log('Body: ', JSON.parse(data));
    });

}).on("error", (err) => {
    console.log("Error: ", err.message);
});

req.write(data);
req.end();  
};

回答1:


source : How to Make an HTTP Post Request using Node.js

const https = require('https');

const data = JSON.stringify({
    name: 'John Doe',
    job: 'Content Writer'
});

const options = {
    hostname: 'reqres.in',
    path: '/api/users',
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        'Content-Length': data.length
    }
};


const req = https.request(options, (res) => {
    let data = '';

    console.log('Status Code:', res.statusCode);

    res.on('data', (chunk) => {
        data += chunk;
    });

    res.on('end', () => {
        console.log('Body: ', JSON.parse(data));
    });

}).on("error", (err) => {
    console.log("Error: ", err.message);
});

req.write(data);
req.end();


来源:https://stackoverflow.com/questions/66007709/how-to-call-a-post-api-from-lambda-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!