如何在Node.js内部进行远程REST调用?任何CURL? [英] How to make remote REST call inside Node.js? any CURL?
本文介绍了如何在Node.js内部进行远程REST调用?任何CURL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 Node.js 中,除了使用子进程进行 CURL 调用,是否有一种方法可以将CURL调用到远程服务器 REST API并获取返回数据?
In Node.js, other than using child process to make CURL call, is there a way to make CURL call to remote server REST API and get the return data?
我还需要将请求标头设置为远程 REST 调用,在GET(或POST)。
I also need to set up the request header to the remote REST call, and also query string as well in GET (or POST).
我找到这个: http://blog.nodejitsu.com/jsdom-jquery-in-5-lines-on-nodejs
但它不不显示任何方式POST查询字符串。
but it doesn't show any way to POST query string.
推荐答案
查看 http.request
var options = {
host: url,
port: 80,
path: '/resource?id=foo&bar=baz',
method: 'POST'
};
http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
}).end();
这篇关于如何在Node.js内部进行远程REST调用?任何CURL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文