博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Node.js 调用 restful webservice
阅读量:4682 次
发布时间:2019-06-09

本文共 2424 字,大约阅读时间需要 8 分钟。

 

如何构建一个restful web service参考原来的文章

http://www.cnblogs.com/ericnie/p/5212748.html

直接用原来的项目编译好像有问题,此处耗费1个半钟头,新建立一个项目就完全OK了 :-(

 

写一个callrest.js,代码如下:

var http = require('http');

var equal = require('assert').equal;

var username = 'falcon';

var password = '';
var _auth = 'Basic ' + new Buffer(username + ':' + password).toString('base64')

var options = {

host: '192.168.0.101',
port: 7001,
path: '/RestfulApplication-testproject-context-root/resources/testproject/Persons',
method: 'GET',
headers:{
'accept': '*/*',
'content-type': "application/xml",
'accept-encoding': 'gzip, deflate',
'accept-language': 'en-US,en;q=0.9',
'user-agent': 'nodejs rest client'
}
};

var req = http.request(options, function (res) {

console.log('STATUS: ' + res.statusCode);
equal(200, res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));

res.on('data',function (chunk) {

console.log('BODY: ' + chunk);
});
});

 

req.on('error', function(e) {

console.log('problem with request: ' + e.message);
});

req.end();

 

 

之前在设置'content-type'"application/atom+xml",结果得到错误

STATUS: 415,查了后是返回的格式不支持,然后修改为application/json或者application/xml就通过了,因为后端restful代码写的是

application/json和application/xml

执行node callrest.js输出如下:

[weblogic@ericnie nodejs-cluster]$ node callrest.js

STATUS: 200
HEADERS: {"connection":"close","date":"Fri, 14 Oct 2016 03:30:31 GMT","content-length":"981","content-type":"application/json"}
BODY: [{"firstname":"Firstname 0","hiredate":"2016-10-14T11:30:31.124+08:00","id":0,"lastname":"Last 0"},{"firstname":"Firstname 1","hiredate":"2016-10-14T11:30:31.124+08:00","id":1,"lastname":"Last 1"},{"firstname":"Firstname 2","hiredate":"2016-10-14T11:30:31.124+08:00","id":2,"lastname":"Last 2"},{"firstname":"Firstname 3","hiredate":"2016-10-14T11:30:31.124+08:00","id":3,"lastname":"Last 3"},{"firstname":"Firstname 4","hiredate":"2016-10-14T11:30:31.124+08:00","id":4,"lastname":"Last 4"},{"firstname":"Firstname 5","hiredate":"2016-10-14T11:30:31.124+08:00","id":5,"lastname":"Last 5"},{"firstname":"Firstname 6","hiredate":"2016-10-14T11:30:31.124+08:00","id":6,"lastname":"Last 6"},{"firstname":"Firstname 7","hiredate":"2016-10-14T11:30:31.124+08:00","id":7,"lastname":"Last 7"},{"firstname":"Firstname 8","hiredate":"2016-10-14T11:30:31.124+08:00","id":8,"lastname":"Last 8"},{"firstname":"Firstname 9","hiredate":"2016-10-14T11:30:31.124+08:00","id":9,"lastname":"Last 9"}]

 

转载于:https://www.cnblogs.com/ericnie/p/5959785.html

你可能感兴趣的文章
比特、字节、K
查看>>
流程控制 Day06
查看>>
Linux下安装Tomcat
查看>>
windows live writer 2012 0x80070643
查看>>
Unity3D Shader 半兰伯特光照模型
查看>>
C程序的启动和终止
查看>>
js的继承
查看>>
AIDL
查看>>
Find a way HDU - 2612
查看>>
DSP与STM32区别
查看>>
String类型方法
查看>>
sass
查看>>
web工程名出现红色的叹号
查看>>
未进入Kali Linux系统修改修改密码的方法
查看>>
SQLServer中的变量:局部变量,全局变量
查看>>
WPF自定义命令
查看>>
P3531 [POI2012]LIT-Letters
查看>>
JS中的正则表达式
查看>>
kafka集群管理工具kafka-manager部署安装
查看>>
Swift 实现俄罗斯方块详细思路解析(附完整项目)
查看>>