我在如何使用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請求 –