用javascript执行curl请求



如果可能的话,有人能帮助我如何用javascript实现这一点吗?

curl 
-H 'Accept: application/json' 
-H 'Client-ID: <client_id>' 
-d'{"channel_id":<channel_id>}' 
-X POST 'https://open-api.trovo.live/openplatform/channels/id'

您可以使用child_process或转换为fetch(或等效(

const exec = require('child_process').exec;
exec('curl ...', function (error, stdout, stderr) {
// handle result
})

为什么要使用JS。

您可以使用JQUERY Ajax:

$.ajax({
url: url,
dataType: "json",
type: "POST/GET",
async: true,
data: { },
success: function (data) {
//code
},
error: function (xhr, exception) {
//console.log err
}
}); 

这是JS Fetch API:

fetch("https://open-api.trovo.live/openplatform/channels/id", {
headers: {
Accept: "application/json",
"Client-Id": "<client_id>"
},
method: "POST"
})

最新更新