我在如何使用node.js中的cURL创建代码时遇到了问题。如何在node.js中执行此cURL代码?
curl -X PUT "URL" \ -H "Authorization: Bearer Authorization code" \ -d "power=on"
我在如何使用node.js中的cURL创建代码时遇到了问题。如何在node.js中执行此cURL代码?
curl -X PUT "URL" \ -H "Authorization: Bearer Authorization code" \ -d "power=on"
可以在node.js中使用运行任意命令的内置child_process模块:
示例代码:
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
模块(如评论中所建议的)或选择任何
此代码对我无效。语法 - 错误 – SGE59
@ SGE59如果您有node.js版本4或更高版本,此代码将可用。你可以在这里玩耍https://runkit.com/holubiev/58f7dd06c59f1f0011952fce –
可以使用内置的nodejs'http'模块或其他许多模块用于制作http请求 –