Node.js 调用 restful webservice
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Node.js 调用 restful webservice相关的知识,希望对你有一定的参考价值。
如何构建一个restful web service参考原来的文章
http://www.cnblogs.com/ericnie/p/5212748.html
直接用原来的项目编译好像有问题,此处耗费1个半钟头,新建立一个项目就完全OK了 :-(
写一个callrest.js,代码如下:
var http = require(‘http‘); var username = ‘falcon‘; var options = { var req = http.request(options, function (res) { res.on(‘data‘,function (chunk) {
req.on(‘error‘, function(e) {
|
之前在设置‘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"}]
以上是关于Node.js 调用 restful webservice的主要内容,如果未能解决你的问题,请参考以下文章
Node.js Lambda 函数从 REST 调用将“响应无效”返回给 Alexa 服务模拟器
允许对 express/node.js 应用程序的 CORS REST 请求