使用带头的节点js进行GET调用
GET call with node js with headers
这是我进行REST调用的代码。我已经尝试过POSTMAN rest调用,url和标头都可以。但使用node js,我无法执行此
我没有在这里给出真正的API密钥和url
/**
* HOW TO Make an HTTP Call - GET
*/
// options for GET
var optionsget = {
host : 'abcd.com:80/edwde/wedwed/', // here only the domain name
// (no http/https !)
//port : 443,
//path : '/youscada', // the rest of the url with parameters if needed
headers : getheaders,
method : 'GET' // do GET
};
var getheaders = {
'x-api-key' : 'diudnwod87wedh8778=',
'content-type' : 'application/json;charset=UTF-8'
};
console.info('Options prepared:');
console.info(optionsget);
console.info('Do the GET call');
// do the GET request
var reqGet = https.request(optionsget, function(res) {
console.log("statusCode: ", res.statusCode);
// uncomment it for header details
console.log("headers: ", res.headers);
res.on('data', function(d) {
console.info('GET result:'n');
process.stdout.write(d);
console.info(''n'nCall completed');
});
});
reqGet.end();
reqGet.on('error', function(e) {
console.error(e);
});
这是我的代码的参考:http://isolasoftware.it/2012/05/28/call-rest-api-with-node-js/
我已经编辑了我的代码,它正在工作:
var http = require('http');
/**
* HOW TO Make an HTTP Call - GET
*/
// options for GET
var getheaders = {
'x-api-key' : 'diudnwod87wedh8778=',
'content-type' : 'application/json;charset=UTF-8'
};
var optionsget = {
host : 'abcd.com', // here only the domain name
// (no http/https !)
port : 80,
path : '/edwde/wedwed/', // the rest of the url with parameters if needed
headers : getheaders,
method : 'GET' // do GET
};
console.info('Options prepared:');
console.info(optionsget);
console.info('Do the GET call');
// do the GET request
var reqGet = http.request(optionsget, function(res) {
console.log("statusCode: ", res.statusCode);
// uncomment it for header details
console.log("headers: ", res.headers);
res.on('data', function(d) {
console.info('GET result:'n');
process.stdout.write(d);
console.info(''n'nCall completed');
});
});
reqGet.end();
reqGet.on('error', function(e) {
console.error(e);
});
输出究竟是什么?此外,在实际声明getheader之前,您可以在optionsget中引用getheader。
您应该查看请求库。
相关文章:
- Adobe Captivate:在SCORM模块中使用GET/POST进行服务器端计算
- 如何检查$httpBackend中是否未进行GET调用
- 在一小段时间的按键事件后,在angular js中进行get调用
- AngularJS:转发到登录页面进行身份验证,保留路由作为GET参数
- 使用 get 元素按 ID 进行用户输入
- 如何在表单提交期间进行 $.get 调用
- 如何使用环回对 GET 响应进行排序
- 如何在nodejs的浏览器中对http GET上的客户端js文件进行模糊处理
- 如何防止 form.target 导致历史记录中的所有 HTTP GET 对服务器进行另一次往返
- 在没有ajax的情况下从javascript对php进行GET调用
- 是否对JavaScript地图对象进行了索引以优化Map.get
- 使用带头的节点js进行GET调用
- 是否可以进行静默/隐藏的GET请求
- 使用$.Ajax进行GET、Update&单击按钮时将JSON放回服务器
- 使用jQuery进行GET请求的问题
- 将本地存储转换为Chrome存储进行get和set
- React路由器:无法将电子邮件地址作为参数进行GET
- 如何通过在终端中执行javascript文件和打印响应来进行GET调用
- 使用AJAX进行GET或POST
- 使用AJAX进行GET和POST