Node.js如何调用restful webservice
沉沙 2018-07-26 来源 : 阅读 1817 评论 0

摘要:本篇Node.js教程探讨了Node.js如何调用restful webservice,希望阅读本篇文章以后大家有所收获,帮助大家对Node.js的理解更加深入。

写一个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相关知识,请关注职坐标WEB前端Node.js频道!

本文由 @沉沙 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程