如何构建一个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"}]

Node.js 调用 restful webservice的更多相关文章

  1. node.js 调用天气webservice接口

    首先安装soap模块 npm install soap 1 2 3 4 5 6 7 8 9 10   var soap = require('soap');   var url = 'http://w ...

  2. Node.js调用百度地图Web服务API的Geocoding接口进行点位反地理信息编码

    (从我的新浪博客上搬来的,做了一些修改.) 最近迷上了node.js以及JavaScript.现在接到一个活,要解析一个出租车点位数据的地理信息.于是就想到使用Node.js调用百度地图API进行解析 ...

  3. 使用Node.js调用阿里云短信的发送以及接收

    为了使用Node.js调用阿里云短信服务,我自己写了个npm包, 目前实现了: 使用Node.js调用阿里云短信服务,发送短信: 使用Node.js调用阿里云短信服务以及MNS服务,接收用户上行短信 ...

  4. Node.js调用C#代码

    在Node.js的项目中假如我们想去调用已经用C#写的dll库该怎么办呢?在这种情况下Edge.js是一个不错的选择,Edge.js是一款在GitHub上开源的技术,它允许Node.js和.NET c ...

  5. windows下node.js调用bat

    node.js调用bat需要用到Child Processes模块 因为bat是文件,所以需要使用execFile方法   如果指定了cwd,它会切换bat执行的目录,类似cd的功能,如果未指定默认为 ...

  6. Node.js实现RESTful api,express or koa?

    文章导读: 一.what's RESTful API 二.Express RESTful API 三.KOA RESTful API 四.express还是koa? 五.参考资料 一.what's R ...

  7. $ npm install opencv ? 你试试?! 在windows环境下,使用node.js调用opencv攻略

    博主之前写过一篇文章<html5与EmguCV前后端实现——人脸识别篇>,叙述的是opencv和C#的故事.最近在公司服务器上更新了一套nodejs环境,早就听闻npm上有opencv模块 ...

  8. 解决Node.js调用fs.renameSync报错的问题(Error: EXDEV, cross-device link not permitted)

    2014-08-23 今天开始学习Node.js,在写一个文件上传的功能时候,调用fs.renameSync方法错误 出错代码所在如下: function upload(response,reques ...

  9. Node.js:RESTful API

    ylbtech-Node.js:RESTful API 1.返回顶部 1. Node.js RESTful API 什么是 REST? REST即表述性状态传递(英文:Representational ...

随机推荐

  1. bzoj 1089 SCOI2003严格n元树 递推

    挺好想的,就是一直没调过,我也不知道哪儿的错,对拍也拍了,因为数据范围小,都快手动对拍了也不知道 哪儿错了.... 我们定义w[i]代表深度<=i的严格n元树的个数 那么最后w[d]-w[d-1 ...

  2. django中管理程序2

    升级版 from os import path TASKS_ROOT = path.dirname(path.abspath(path.dirname(__file__))) PYTHON_ROOT ...

  3. LeetCode 6:Excel Sheet Column Number

    Given a column title as appear in an Excel sheet, return its corresponding column number. For exampl ...

  4. python基础===python3 get和post请求(转载)

    get请求 #encoding:UTF-8 importurllib importurllib.request data={} data['name']='aaa' url_parame=urllib ...

  5. Guava Cache相关

    官方:http://ifeve.com/google-guava-cachesexplained/ 理解:https://segmentfault.com/a/1190000007300118 项目中 ...

  6. Flex slider参数详细

    $(window).load(function() { $('.flexslider').flexslider({ animation: "fade", //String: Sel ...

  7. vmware的3种网络模式

    ####图片以及部分内容来源:https://note.youdao.com/share/?id=236896997b6ffbaa8e0d92eacd13abbf&type=note#/ 在安 ...

  8. python中的闭包与装饰器

    #原创,转载请留言联系 装饰器的本质就是闭包,所以想知道装饰器是什么,首先要理解一下什么是闭包. 闭包 1. 外部函数返回内部函数的引用.2. 内部函数使用外部函数的变量或者参数. def outer ...

  9. quote(),unquote(),urlencode()编码解码

    quote(),unquote(),quote_plus(),unquote_plus(),urlencode() ,pathname2url(),url2pathname() urllib中还提供了 ...

  10. js的变量的有效域

    function test(o) { var i=0; if(typeof o=="object") { var j=1; for(var k=0;k<10;k++) { c ...