【问题标题】:How to use curl -F in nodejs?如何在 nodejs 中使用 curl -F?
【发布时间】:2015-06-27 16:38:54
【问题描述】:

我正在尝试建立一个与 Instagram 连接的网站。

我想实时订阅 Instagram 的标签。

curl -F 'client_id=CLIENT-ID' \
 -F 'client_secret=CLIENT-SECRET' \
 -F 'object=tag' \
 -F 'aspect=media' \
 -F 'object_id=nofilter' \
 -F 'callback_url=http://YOUR-CALLBACK/URL' \
 https://api.instagram.com/v1/subscriptions/

我真的不知道如何在 nodejs 中做到这一点。

【问题讨论】:

  • 到目前为止你有什么?
  • require('request') 将是一个开始
  • 我认为您真的在寻找如何发出请求,而不是如何在 nodejs 中使用 curl,因为我认为您不能在 nodejs 中使用 curl ...

标签: node.js


【解决方案1】:

您需要以下代码的请求库。 https://github.com/request/request

var request = require('request');

var formData = {
  client_secret: "CLIENT-SECRET",
  object: "tag",
  aspect: "media",
  "object_id": "nofilter",
  "callback_url": "http://YOUR-CALLBACK/URL"
};

request.post({
  url: "https://api.instagram.com/v1/subscriptions/",
  formData: formData
}, function (err, res, body) {
  if (err) {
    console.log(err);
  } else {
    console.log(body);
  }
});

【讨论】:

    【解决方案2】:

    正如其他人所说,请求可能是最简单的解决方案。但是,如果您必须直接使用 curl,则以下内容可以原生使用:

    var spawn = require('child_process').spawn;
    
    var command = spawn('curl', []);
    
    command.stderr.on('data', function(data) { });
    
    command.stdout.on('data', function(data) { });
    
    command.stdout.on('close', function() {  });
    

    请参阅spawn command arguments 了解更多信息。或者,node-curl 可用。

    【讨论】:

      猜你喜欢
      • 2018-06-25
      • 1970-01-01
      • 2019-09-18
      • 2022-01-23
      • 2015-01-27
      • 1970-01-01
      • 2023-03-16
      • 2017-10-17
      • 1970-01-01
      相关资源
      最近更新 更多