【问题标题】:Can someone help me convert this Curl request into node.js?有人可以帮我将此 Curl 请求转换为 node.js 吗?
【发布时间】:2018-05-16 17:39:46
【问题描述】:

我正在使用 Webhooks,并尝试从我的 node.js 代码运行 Curl 请求。我正在使用 npm request 包来执行此操作。我无法找到将 Curl 请求转换为将发送请求的应用程序中的代码的正确方法。

这是 Curl 请求:

curl -X POST https://tartan.plaid.com/connect \
  -d client_id=test_id \
  -d secret=test_secret \
  -d username=plaid_test \
  -d password=plaid_good \
  -d type=wells \
  -d options='{
      "webhook":"http://requestb.in/",
      "login_only":true }'

当我在终端中运行它时它工作正常,所以我知道凭据有效并且它正在与服务器通信。

这是我的 Node.js 代码:

var request = require('request');

var opt = {
  url: 'https://tartan.plaid.com/connect',
  data: {
    'client_id': 'test_id',
    'secret': 'test_secret',
    'username': 'plaid_test',
    'password': 'plaid_good',
    'type': 'wells',
    'webhook': 'http://requestb.in/', 
    'login_only': true
  }
};

request(opt, function (error, response, body) {
  console.log(body)
});

它应该返回一个 item 但我得到的只是:

{
  "code": 1100,
  "message": "client_id missing",
  "resolve": "Include your Client ID so we know who you are."
}

所有凭据都来自 Plaid 网站,它们在我的终端上运行良好,所以我认为这正是我编写 Node.js 代码的方式导致了问题。

如果有人可以帮助我找到编写节点代码的正确方法,以便它执行 curl 请求在终端中执行的操作,那将不胜感激!谢谢!

【问题讨论】:

    标签: node.js curl plaid


    【解决方案1】:

    您可能希望在选项中使用form: 而不是data:。希望这能解决问题。

    【讨论】:

      【解决方案2】:

      request 的默认方法是 GET。你想要一个 POST,所以你必须将它设置为参数。您还必须根据documentation 将数据作为 JSON 发送。所以我相信这应该可行:

      var opt = {
        url: 'https://tartan.plaid.com/connect',
        method: "POST",
        json: {
          'client_id': 'test_id',
          'secret': 'test_secret',
          'username': 'plaid_test',
          'password': 'plaid_good',
          'type': 'wells',
          'webhook': 'http://requestb.in/', 
          'login_only': true
        }
      };
      

      【讨论】:

        【解决方案3】:

        请参阅explainshell: curl -X -d,了解您的 curl 命令实际执行的操作。

        • 您发送 POST 请求
        • 您使用内容类型发送数据application/x-www-form-urlencoded

        要使用 request 复制它,您必须相应地配置它:

        var opt = {
          url: 'https://tartan.plaid.com/connect',
          form: {
            // ...
          }
        };
        
        request.post(opt, function (error, response, body) {
          console.log(body)
        });
        

        更多示例请参见application/x-www-form-urlencoded

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-10-31
          • 1970-01-01
          • 2015-08-10
          • 2016-04-12
          • 2018-06-07
          • 2021-07-08
          • 1970-01-01
          • 2018-01-30
          相关资源
          最近更新 更多