node-rest-client
Advanced tools
Comparing version 1.5.1 to 1.8.0
@@ -149,6 +149,17 @@ var http = require('http'), | ||
// create enconded URL from args | ||
for (var key in args){ | ||
var keyValue = key + "=" + encodeURIComponent(args[key]); | ||
if (counter > 1) keyValue = '&'.concat(keyValue); | ||
result = result.concat(keyValue); | ||
for (var key in args) { | ||
var keyValue = ""; | ||
if ( args[key] instanceof Array ) { | ||
/* | ||
* We are dealing with an array in the query string ?key=Value0&key=Value1 | ||
* That a REST application translates into key=[Value0, Value1] | ||
*/ | ||
for ( var ii=0, sizeArray = args[key].length; ii < sizeArray; ii++ ) { | ||
result = result.concat((counter > 1 ? "&": "") + key + "=" + encodeURIComponent(args[key][ii])); | ||
counter++; | ||
} | ||
} else { //No array, just a single &key=value | ||
keyValue = key + "=" + encodeURIComponent(args[key]); | ||
result = result.concat((counter > 1 ? "&":"") + keyValue); | ||
} | ||
@@ -415,3 +426,3 @@ counter++; | ||
"handleResponse":function(res,data,callback){ | ||
var content = res.headers["content-type"]; | ||
var content = res.headers["content-type"] && res.headers["content-type"].replace(/ /g, ''); | ||
@@ -519,3 +530,3 @@ debug("response content is ",content); | ||
// write POST/PUT data to request body; | ||
if(options.data) request.write(this.prepareData(options.data)); | ||
if(options.data) request.write(self.prepareData(options.data)); | ||
@@ -522,0 +533,0 @@ |
@@ -5,3 +5,3 @@ { | ||
"description": "node API REST client", | ||
"version": "1.5.1", | ||
"version": "1.8.0", | ||
"repository": { | ||
@@ -11,6 +11,6 @@ "type":"git", | ||
}, | ||
"main": "./lib/node-rest-client", | ||
"main": "./lib/node-rest-client", | ||
"dependencies": { | ||
"xml2js":">=0.2.4", | ||
"debug": "~2.1.1" | ||
"debug": "~2.2.0" | ||
}, | ||
@@ -23,3 +23,4 @@ "devDependencies": { | ||
"node": "*" | ||
} | ||
}, | ||
"license": "MIT" | ||
} |
373
readme.md
@@ -31,11 +31,11 @@ # REST Client for Node.js | ||
client = new Client(); | ||
var client = new Client(); | ||
// direct way | ||
client.get("http://remote.site/rest/xml/method", function(data, response){ | ||
// parsed response body as js object | ||
console.log(data); | ||
// raw response | ||
console.log(response); | ||
}); | ||
client.get("http://remote.site/rest/xml/method", function (data, response) { | ||
// parsed response body as js object | ||
console.log(data); | ||
// raw response | ||
console.log(response); | ||
}); | ||
@@ -45,3 +45,3 @@ // registering remote methods | ||
client.methods.jsonMethod(function(data,response){ | ||
client.methods.jsonMethod(function (data, response) { | ||
// parsed response body as js object | ||
@@ -52,3 +52,2 @@ console.log(data); | ||
}); | ||
``` | ||
@@ -61,3 +60,2 @@ | ||
```javascript | ||
//Example POST method invocation | ||
@@ -70,8 +68,8 @@ var Client = require('node-rest-client').Client; | ||
var args = { | ||
data: { test: "hello" }, | ||
headers:{"Content-Type": "application/json"} | ||
data: { test: "hello" }, | ||
headers: { "Content-Type": "application/json" } | ||
}; | ||
client.post("http://remote.site/rest/xml/method", args, function(data,response) { | ||
// parsed response body as js object | ||
client.post("http://remote.site/rest/xml/method", args, function (data, response) { | ||
// parsed response body as js object | ||
console.log(data); | ||
@@ -85,3 +83,3 @@ // raw response | ||
client.methods.postMethod(args, function(data,response){ | ||
client.methods.postMethod(args, function (data, response) { | ||
// parsed response body as js object | ||
@@ -92,3 +90,2 @@ console.log(data); | ||
}); | ||
``` | ||
@@ -106,19 +103,19 @@ If no "Content-Type" header is set as client arg POST,PUT and PATCH methods will not work properly. | ||
// direct way | ||
client = new Client(); | ||
var client = new Client(); | ||
args ={ | ||
data:{test:"hello"}, // data passed to REST method (only useful in POST, PUT or PATCH methods) | ||
path:{"id":120}, // path substitution var | ||
parameters:{arg1:"hello",arg2:"world"}, // query parameter substitution vars | ||
headers:{"test-header":"client-api"} // request headers | ||
}; | ||
var args = { | ||
data: { test: "hello" }, // data passed to REST method (only useful in POST, PUT or PATCH methods) | ||
path: { "id": 120 }, // path substitution var | ||
parameters: { arg1: "hello", arg2: "world" }, // query parameter substitution vars | ||
headers: { "test-header": "client-api" } // request headers | ||
}; | ||
client.get("http://remote.site/rest/json/${id}/method?arg1=hello&arg2=world", args, | ||
function(data, response){ | ||
// parsed response body as js object | ||
console.log(data); | ||
// raw response | ||
console.log(response); | ||
}); | ||
client.get("http://remote.site/rest/json/${id}/method?arg1=hello&arg2=world", args, | ||
function (data, response) { | ||
// parsed response body as js object | ||
console.log(data); | ||
// raw response | ||
console.log(response); | ||
}); | ||
@@ -130,3 +127,2 @@ | ||
/* this would construct the following URL before invocation | ||
@@ -136,4 +132,4 @@ * | ||
* | ||
*/ | ||
client.methods.jsonMethod(args,function(data,response){ | ||
*/ | ||
client.methods.jsonMethod(args, function (data, response) { | ||
// parsed response body as js object | ||
@@ -144,3 +140,2 @@ console.log(data); | ||
}); | ||
``` | ||
@@ -154,18 +149,17 @@ | ||
// direct way | ||
client = new Client(); | ||
var client = new Client(); | ||
args ={ | ||
path:{"id":120,"arg1":"hello","arg2":"world"}, | ||
parameters:{arg1:"hello",arg2:"world"}, | ||
headers:{"test-header":"client-api"} | ||
}; | ||
var args = { | ||
path: { "id": 120, "arg1": "hello", "arg2": "world" }, | ||
parameters: { arg1: "hello", arg2: "world" }, | ||
headers: { "test-header": "client-api" } | ||
}; | ||
client.get("http://remote.site/rest/json/${id}/method?arg1=${arg1}&arg2=${arg2}", args, | ||
function(data, response){ | ||
// parsed response body as js object | ||
console.log(data); | ||
// raw response | ||
console.log(response); | ||
}); | ||
client.get("http://remote.site/rest/json/${id}/method?arg1=${arg1}&arg2=${arg2}", args, | ||
function (data, response) { | ||
// parsed response body as js object | ||
console.log(data); | ||
// raw response | ||
console.log(response); | ||
}); | ||
``` | ||
@@ -183,16 +177,16 @@ | ||
// direct way | ||
client = new Client(); | ||
var client = new Client(); | ||
args ={ | ||
path:{"id":120}, | ||
parameters:{arg1:"hello",arg2:"world"}, | ||
headers:{"test-header":"client-api"}, | ||
data:"<xml><arg1>hello</arg1><arg2>world</arg2></xml>" | ||
}; | ||
var args = { | ||
path: { "id": 120 }, | ||
parameters: { arg1: "hello", arg2: "world" }, | ||
headers: { "test-header": "client-api" }, | ||
data: "<xml><arg1>hello</arg1><arg2>world</arg2></xml>" | ||
}; | ||
client.post("http://remote.site/rest/xml/${id}/method?arg1=hello&arg2=world", args, function(data, response){ | ||
// parsed response body as js object | ||
console.log(data); | ||
// raw response | ||
console.log(response); | ||
client.post("http://remote.site/rest/xml/${id}/method?arg1=hello&arg2=world", args, function (data, response) { | ||
// parsed response body as js object | ||
console.log(data); | ||
// raw response | ||
console.log(response); | ||
}); | ||
@@ -204,3 +198,3 @@ | ||
client.methods.xmlMethod(args,function(data,response){ | ||
client.methods.xmlMethod(args, function (data, response) { | ||
// parsed response body as js object | ||
@@ -213,10 +207,10 @@ console.log(data); | ||
// posted data can be js object | ||
args_js ={ | ||
path:{"id":120}, | ||
parameters:{arg1:"hello",arg2:"world"}, | ||
headers:{"test-header":"client-api"}, | ||
data:{"arg1":"hello","arg2":123} | ||
}; | ||
var args_js = { | ||
path: { "id": 120 }, | ||
parameters: { arg1: "hello", arg2: "world" }, | ||
headers: { "test-header": "client-api" }, | ||
data: { "arg1": "hello", "arg2": 123 } | ||
}; | ||
client.methods.xmlMethod(args_js,function(data,response){ | ||
client.methods.xmlMethod(args_js, function (data, response) { | ||
// parsed response body as js object | ||
@@ -227,3 +221,2 @@ console.log(data); | ||
}); | ||
``` | ||
@@ -237,30 +230,28 @@ | ||
```javascript | ||
var client = new Client(); | ||
// request and response additional configuration | ||
args ={ | ||
path:{"id":120}, | ||
parameters:{arg1:"hello",arg2:"world"}, | ||
headers:{"test-header":"client-api"}, | ||
data:"<xml><arg1>hello</arg1><arg2>world</arg2></xml>", | ||
requestConfig:{ | ||
timeout:1000, //request timeout in milliseconds | ||
noDelay:true, //Enable/disable the Nagle algorithm | ||
keepAlive:true, //Enable/disable keep-alive functionalityidle socket. | ||
keepAliveDelay:1000 //and optionally set the initial delay before the first keepalive probe is sent | ||
}, | ||
responseConfig:{ | ||
timeout:1000 //response timeout | ||
} | ||
}; | ||
var args = { | ||
path: { "id": 120 }, | ||
parameters: { arg1: "hello", arg2: "world" }, | ||
headers: { "test-header": "client-api" }, | ||
data: "<xml><arg1>hello</arg1><arg2>world</arg2></xml>", | ||
requestConfig: { | ||
timeout: 1000, //request timeout in milliseconds | ||
noDelay: true, //Enable/disable the Nagle algorithm | ||
keepAlive: true, //Enable/disable keep-alive functionalityidle socket. | ||
keepAliveDelay: 1000 //and optionally set the initial delay before the first keepalive probe is sent | ||
}, | ||
responseConfig: { | ||
timeout: 1000 //response timeout | ||
} | ||
}; | ||
client.post("http://remote.site/rest/xml/${id}/method?arg1=hello&arg2=world", args, function(data, response){ | ||
// parsed response body as js object | ||
console.log(data); | ||
// raw response | ||
console.log(response); | ||
client.post("http://remote.site/rest/xml/${id}/method?arg1=hello&arg2=world", args, function (data, response) { | ||
// parsed response body as js object | ||
console.log(data); | ||
// raw response | ||
console.log(response); | ||
}); | ||
``` | ||
@@ -274,27 +265,27 @@ If you want to handle timeout events both in the request and in the response just add a new "requestTimeout" | ||
// request and response additional configuration | ||
args ={ | ||
path:{"id":120}, | ||
parameters:{arg1:"hello",arg2:"world"}, | ||
headers:{"test-header":"client-api"}, | ||
data:"<xml><arg1>hello</arg1><arg2>world</arg2></xml>", | ||
requestConfig:{ | ||
timeout:1000, //request timeout in milliseconds | ||
noDelay:true, //Enable/disable the Nagle algorithm | ||
keepAlive:true, //Enable/disable keep-alive functionalityidle socket. | ||
keepAliveDelay:1000 //and optionally set the initial delay before the first keepalive probe is sent | ||
}, | ||
responseConfig:{ | ||
timeout:1000 //response timeout | ||
} | ||
}; | ||
var args = { | ||
path: { "id": 120 }, | ||
parameters: { arg1: "hello", arg2: "world" }, | ||
headers: { "test-header": "client-api" }, | ||
data: "<xml><arg1>hello</arg1><arg2>world</arg2></xml>", | ||
requestConfig: { | ||
timeout: 1000, //request timeout in milliseconds | ||
noDelay: true, //Enable/disable the Nagle algorithm | ||
keepAlive: true, //Enable/disable keep-alive functionalityidle socket. | ||
keepAliveDelay: 1000 //and optionally set the initial delay before the first keepalive probe is sent | ||
}, | ||
responseConfig: { | ||
timeout: 1000 //response timeout | ||
} | ||
}; | ||
var req = client.post("http://remote.site/rest/xml/${id}/method?arg1=hello&arg2=world", args, function(data, response){ | ||
// parsed response body as js object | ||
console.log(data); | ||
// raw response | ||
console.log(response); | ||
var req = client.post("http://remote.site/rest/xml/${id}/method?arg1=hello&arg2=world", args, function (data, response) { | ||
// parsed response body as js object | ||
console.log(data); | ||
// raw response | ||
console.log(response); | ||
}); | ||
req.on('requestTimeout',function(req){ | ||
req.on('requestTimeout', function (req) { | ||
console.log('request has expired'); | ||
@@ -304,12 +295,11 @@ req.abort(); | ||
req.on('responseTimeout',function(res){ | ||
req.on('responseTimeout', function (res) { | ||
console.log('response has expired'); | ||
}); | ||
//it's usefull to handle request errors to avoid, for example, socket hang up errors on request timeouts | ||
req.on('error', function(err){ | ||
console.log('request error',err); | ||
req.on('error', function (err) { | ||
console.log('request error', err); | ||
}); | ||
``` | ||
@@ -327,14 +317,13 @@ | ||
// configure proxy | ||
var options_proxy={ | ||
proxy:{ | ||
host:"proxy.foo.com", | ||
port:8080, | ||
user:"proxyuser", | ||
password:"123", | ||
tunnel:true | ||
} | ||
}, | ||
var options_proxy = { | ||
proxy: { | ||
host: "proxy.foo.com", | ||
port: 8080, | ||
user: "proxyuser", | ||
password: "123", | ||
tunnel: true | ||
} | ||
}; | ||
client = new Client(options_proxy); | ||
var client = new Client(options_proxy); | ||
``` | ||
@@ -349,14 +338,13 @@ | ||
// configure proxy | ||
var options_proxy={ | ||
proxy:{ | ||
host:"proxy.foo.com", | ||
port:8080, | ||
user:"proxyuser", | ||
password:"123", | ||
tunnel:false // use direct request to proxy | ||
} | ||
}, | ||
var options_proxy = { | ||
proxy: { | ||
host: "proxy.foo.com", | ||
port: 8080, | ||
user: "proxyuser", | ||
password: "123", | ||
tunnel: false // use direct request to proxy | ||
} | ||
}; | ||
client = new Client(options_proxy); | ||
var client = new Client(options_proxy); | ||
``` | ||
@@ -374,6 +362,5 @@ | ||
// configure basic http auth for every request | ||
var options_auth={user:"admin",password:"123"}; | ||
var options_auth = { user: "admin", password: "123" }; | ||
client = new Client(options_auth); | ||
var client = new Client(options_auth); | ||
``` | ||
@@ -386,36 +373,34 @@ | ||
```javascript | ||
var options ={ | ||
var options = { | ||
// proxy configuration | ||
proxy:{ | ||
host:"proxy.foo.com", // proxy host | ||
port:8080, // proxy port | ||
user:"ellen", // proxy username if required | ||
password:"ripley" // proxy pass if required | ||
}, | ||
proxy: { | ||
host: "proxy.foo.com", // proxy host | ||
port: 8080, // proxy port | ||
user: "ellen", // proxy username if required | ||
password: "ripley" // proxy pass if required | ||
}, | ||
// aditional connection options passed to node http.request y https.request methods | ||
// (ie: options to connect to IIS with SSL) | ||
connection:{ | ||
connection: { | ||
secureOptions: constants.SSL_OP_NO_TLSv1_2, | ||
ciphers:'ECDHE-RSA-AES256-SHA:AES256-SHA:RC4-SHA:RC4:HIGH:!MD5:!aNULL:!EDH:!AESGCM', | ||
ciphers: 'ECDHE-RSA-AES256-SHA:AES256-SHA:RC4-SHA:RC4:HIGH:!MD5:!aNULL:!EDH:!AESGCM', | ||
honorCipherOrder: true | ||
}, | ||
// customize mime types for json or xml connections | ||
mimetypes:{ | ||
json:["application/json","application/json;charset=utf-8"], | ||
xml:["application/xml","application/xml;charset=utf-8"] | ||
}, | ||
user:"admin", // basic http auth username if required | ||
password:"123", // basic http auth password if required | ||
requestConfig:{ | ||
timeout:1000, //request timeout in milliseconds | ||
noDelay:true, //Enable/disable the Nagle algorithm | ||
keepAlive:true, //Enable/disable keep-alive functionalityidle socket. | ||
keepAliveDelay:1000 //and optionally set the initial delay before the first keepalive probe is sent | ||
mimetypes: { | ||
json: ["application/json", "application/json;charset=utf-8"], | ||
xml: ["application/xml", "application/xml;charset=utf-8"] | ||
}, | ||
responseConfig:{ | ||
timeout:1000 //response timeout | ||
user: "admin", // basic http auth username if required | ||
password: "123", // basic http auth password if required | ||
requestConfig: { | ||
timeout: 1000, //request timeout in milliseconds | ||
noDelay: true, //Enable/disable the Nagle algorithm | ||
keepAlive: true, //Enable/disable keep-alive functionalityidle socket. | ||
keepAliveDelay: 1000 //and optionally set the initial delay before the first keepalive probe is sent | ||
}, | ||
responseConfig: { | ||
timeout: 1000 //response timeout | ||
} | ||
}; | ||
``` | ||
@@ -433,16 +418,16 @@ Note that requestConfig and responseConfig options if set on client instantiation apply to all of its requests/responses | ||
client = new Client(); | ||
var client = new Client(); | ||
var args={ | ||
requesConfig:{timeout:1000}, | ||
responseConfig:{timeout:2000} | ||
}; | ||
var args = { | ||
requesConfig: { timeout: 1000 }, | ||
responseConfig: { timeout: 2000 } | ||
}; | ||
// direct way | ||
var req1 = client.get("http://remote.site/rest/xml/method",args, function(data, response){ | ||
// parsed response body as js object | ||
console.log(data); | ||
// raw response | ||
console.log(response); | ||
}); | ||
var req1 = client.get("http://remote.site/rest/xml/method", args, function (data, response) { | ||
// parsed response body as js object | ||
console.log(data); | ||
// raw response | ||
console.log(response); | ||
}); | ||
@@ -453,3 +438,3 @@ // view req1 options | ||
req1.on('requestTimeout',function(req){ | ||
req1.on('requestTimeout', function (req) { | ||
console.log("request has expired"); | ||
@@ -459,5 +444,5 @@ req.abort(); | ||
req1.on('responseTimeout',function(res){ | ||
req1.on('responseTimeout', function (res) { | ||
console.log("response has expired"); | ||
}); | ||
@@ -469,3 +454,3 @@ | ||
var req2=client.methods.jsonMethod(function(data,response){ | ||
var req2 = client.methods.jsonMethod(function (data, response) { | ||
// parsed response body as js object | ||
@@ -478,3 +463,3 @@ console.log(data); | ||
// handling specific req2 errors | ||
req2.on('error',function(err){ | ||
req2.on('error', function (err) { | ||
console.log('something went wrong on req2!!', err.request.options); | ||
@@ -489,20 +474,18 @@ }); | ||
```javascript | ||
var client = new Client(options_auth); | ||
client = new Client(options_auth); | ||
// handling request error events | ||
client.get("http://remote.site/rest/xml/method", function(data, response){ | ||
// parsed response body as js object | ||
console.log(data); | ||
// raw response | ||
console.log(response); | ||
}).on('error',function(err){ | ||
console.log('something went wrong on the request', err.request.options); | ||
}); | ||
client.get("http://remote.site/rest/xml/method", function (data, response) { | ||
// parsed response body as js object | ||
console.log(data); | ||
// raw response | ||
console.log(response); | ||
}).on('error', function (err) { | ||
console.log('something went wrong on the request', err.request.options); | ||
}); | ||
// handling client error events | ||
client.on('error',function(err){ | ||
client.on('error', function (err) { | ||
console.error('Something went wrong on the client', err); | ||
}); | ||
``` | ||
``` |
@@ -52,2 +52,8 @@ var http = require('http'), | ||
res.end(); | ||
}, | ||
"/json/contenttypewithspace":function(req,res){ | ||
var message = fs.readFileSync('./message.json','utf8'); | ||
res.writeHead(200, {'Content-Type': 'application/json; charset=utf-8'}); | ||
res.write(message.toString()); | ||
res.end(); | ||
} | ||
@@ -54,0 +60,0 @@ }, |
43433
724
463
+ Addeddebug@2.2.0(transitive)
+ Addedms@0.7.1(transitive)
- Removeddebug@2.1.3(transitive)
- Removedms@0.7.0(transitive)
Updateddebug@~2.2.0