【问题标题】:How can I do this cURL code in node.js?如何在 node.js 中执行此 cURL 代码?
【发布时间】:2017-04-20 05:11:02
【问题描述】:

我在如何从 node.js 中的 cURL 制作此代码时遇到问题。

curl -X PUT "URL" \ -H "Authorization: Bearer Authorization code" \ -d "power=on"

【问题讨论】:

  • 您可以使用内置的 nodejs http 模块或许多其他模块来发出 http 请求

标签: node.js http url curl put


【解决方案1】:

您可以使用内置的child_process 模块在 node.js 中运行任意命令:

示例代码:

const {execSync} = require('child_process');
const command = `curl -X PUT "https://google.com"  -H "Authorization: Bearer Authorization code" -d "power=on"`;

execSync(command).toString();

顺便说一句,这不是最好的方法。通常,您应该避免同步调用,因为它们会阻塞事件循环。在您的特定示例中,最好使用内置的 http 模块(如 cmets 中的建议)或选择任何 http module on npm 发出 PUT HTTP 请求

【讨论】:

猜你喜欢
  • 2011-11-21
  • 2015-04-29
  • 1970-01-01
  • 2014-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-27
相关资源
最近更新 更多