【发布时间】: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 中的 cURL 制作此代码时遇到问题。
curl -X PUT "URL" \
-H "Authorization: Bearer Authorization code" \
-d "power=on"
【问题讨论】:
http 模块或许多其他模块来发出 http 请求
您可以使用内置的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 请求
【讨论】: