如何从lambda函数调用POST API? [英] How to call a POST API from lambda function?

查看:59
本文介绍了如何从lambda函数调用POST API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Node.js 12.x从lambda函数集成POST API调用.

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

我尝试如下:

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)"
  ]
}

这里的posturl是我的api路径,而jsondata是我的键值对数据.

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

那么如何从lambda函数调用POST API?调用API时如何传递整个jsondata密钥?服务呼叫后如何解析响应?

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?

更新:我已经尝试过以下操作

我所有的详细信息都通过键jsonData传递,我可以在哪里指定?如果没有该密钥,它将无法正常工作.

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();  
};

推荐答案

源:如何使用Node.js发出HTTP发布请求

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();

这篇关于如何从lambda函数调用POST API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆