javascript - 如何在 node.js 中执行此 curl 操作

标签 javascript node.js asynchronous

我想做的是 node.js 中的这个 curl 操作。

curl -XPOST localhost:12060/repository/schema/fieldType -H 'Content-Type: application/json' -d '
{
  action: "create",
  fieldType: {
    name: "n$name",
    valueType: { primitive: "STRING" },
    scope: "versioned",
    namespaces: { "my.demo": "n" }
  }
}' -D -

欢迎提出建议。

最佳答案

通过这里http://query7.com/nodejs-curl-tutorial

Although there are no specific NodeJS bindings for cURL, we can still issue cURL requests via the command line interface. NodeJS comes with the child_process module which easily allows us to start processes and read their output. Doing so is fairly straight forward. We just need to import the exec method from the child_process module and call it. The first parameter is the command we want to execute and the second is a callback function that accepts error, stdout, stderr.

var util = require('util');
var exec = require('child_process').exec;

var command = 'curl -sL -w "%{http_code} %{time_total}\\n" "http://query7.com" -o /dev/null'

child = exec(command, function(error, stdout, stderr){

console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);

if(error !== null)
{
    console.log('exec error: ' + error);
}

});

编辑这也是一个可能的解决方案:https://github.com/dhruvbird/http-sync

关于javascript - 如何在 node.js 中执行此 curl 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7498648/

相关文章:

javascript - Selenium 刷新任何页面直到元素可见 Java

javascript - 在路由中使用 socket.io

node.js - 根据条件更新 dynamodb 项目

c# - 同时处理数千个数据库调用

javascript - JavaScript 可以在 window.open 上设置 mime 类型吗

javascript - 使用 jQuery 访问按钮的替代文本

javascript - 在 Javascript 中调用函数时,对象和参数列表之间是否有自动映射?

javascript - JSON.parse 将空数组附加到字符串参数的末尾

c# - 'async' 调用图中的最后一次调用是否需要同步?

ios - 异步环境中的同步调用