Node.js 调用 restful webservice

如何构建一个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输出如下:

[[email protected] 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"}]

时间: 2024-08-09 06:59:13

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

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

(从我的新浪博客上搬来的,做了一些修改.) 最近迷上了node.js以及JavaScript.现在接到一个活,要解析一个出租车点位数据的地理信息.于是就想到使用Node.js调用百度地图API进行解析. 使用的库主要就是有fs.request. // 请求包 var fs = require('fs');var request = require('request'); // 设置百度API的参数var baiduApiKey = "cQV9U4QhamoOjg6rjdOTAQSiUMxxxxx

Node.js调用C#代码

在Node.js的项目中假如我们想去调用已经用C#写的dll库该怎么办呢?在这种情况下Edge.js是一个不错的选择,Edge.js是一款在GitHub上开源的技术,它允许Node.js和.NET core在同一个进程内相互调用,并且支持Windows,MacOS和Linux.本地可以通过npm直接安装Edge.js,地址:https://www.npmjs.com/package/edge#windows,上面有关于它的详细介绍,里面有好多的使用情况,下文主要简单介绍其中的一种使用方法来让No

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

2014-08-23 今天开始学习Node.js,在写一个文件上传的功能时候,调用fs.renameSync方法错误 出错代码所在如下: 1 function upload(response,request){ 2 console.log("upload called"); 3 var form = new formidable.IncomingForm(); 4 console.log("about to parse"); 5 form.parse(request

node.js调用ffi

原帖 例一:调用系统的user32.dll函数 MessageBoxW,有4个参数,输出一个整数 , var FFI = require('ffi'); function TEXT(text){ return new Buffer(text, 'ucs2').toString('binary'); } var user32 = new FFI.Library('user32', { 'MessageBoxW': [ 'int32', [ 'int32', 'string', 'string',

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

文章导读: 一.what's RESTful API 二.Express RESTful API 三.KOA RESTful API 四.express还是koa? 五.参考资料 一.what's RESTful API 1.1 RESTful架构 要理解什么是RESTful API我们可以先看一下什么是RESTful架构. REST是Representational State Transfer的缩写,我们可以理解为它的含义是“表现层状态转化”,wikipedia是这样说的:“an archi

node.js 调用mysql 数据库

1.在package.json中添加mysql依赖 命令:npm install mysql --save 2.项目中添加mysql文件夹 > 文件夹下创建config文件夹,并在config下创建index.js写入mysql配置文件 const mysql = require('mysql') const connectdb=()=>{ let connection = mysql.createConnection({ host : 'localhost', port : '3306',

node.js 调用第三方服务

node作为客户端调用第三方服务   nodejs.cn/api 1. let http = require('http'); let util = require("util"); http.get('http://www.imooc.com/u/card',(res)=>{ let data = ""'; res.on("data",(chunk)=>{ data += chunk; }); res.on("end&qu

java调用restful webservice(转)

一般来说,大家只会用到GET和POST方法来调用. GET方法的话,参数可以写在url里面. 比如说server的interface用的是@RequestParam或者@PathVariable,在客户端调用的时候,都可以直接写在URL里,具体写法我就不写了,和下面差不多. POST方法.这个有点不一样,参数要写在request 的body里面,而不是URL里面. URL = url = new URL(http://test.webservice.api/test); HttpURLConne

Node.js学习 - RESTFul API

REST Representational State Transfer (表述性状态转移), 是一组架构约束条件和原则.满足这些约束条件和原则的应用程序或设计就是RESTful. RESTful Web Services Web service是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述.发布.发现.协调和配置这些应用程序,用于开发分布式的互操作的应用程序. 基于 REST 架构的 Web Services 即