node-rest-client
Advanced tools
Comparing version 2.0.1 to 2.5.0
@@ -1,3 +0,3 @@ | ||
var http = require('http'), | ||
https = require('https'), | ||
var http = require('follow-redirects').http, | ||
https = require('follow-redirects').https, | ||
parseString = require('xml2js').parseString, | ||
@@ -13,3 +13,8 @@ urlParser = require('url'), | ||
var connectManager = new ConnectManager(); | ||
//declare util constants | ||
var CONSTANTS={ | ||
HEADER_CONTENT_LENGTH:"Content-Length" | ||
}; | ||
self.options = options || {}, | ||
@@ -20,4 +25,4 @@ self.useProxy = (self.options.proxy || false)?true:false, | ||
self.connection = self.options.connection || {}, | ||
self.mimetypes = self.options.mimetypes || {}; | ||
self.requestConfig = self.options.requestConfig || {}; | ||
self.mimetypes = self.options.mimetypes || {}, | ||
self.requestConfig = self.options.requestConfig || {}, | ||
self.responseConfig = self.options.responseConfig || {}; | ||
@@ -47,4 +52,5 @@ | ||
var Util = { | ||
createProxyPath:function(url){ | ||
@@ -84,3 +90,4 @@ var result = url.host; | ||
path: url.path, | ||
protocol:protocol | ||
protocol:protocol, | ||
href:url.href | ||
}; | ||
@@ -95,3 +102,3 @@ | ||
// some sites only needs user with no password to authenticate | ||
result.auth = self.options.user; | ||
result.auth = self.options.user + ":"; | ||
} | ||
@@ -209,2 +216,3 @@ | ||
options.method = method, | ||
clientRequest.href=options.href, | ||
options.clientRequest = clientRequest, | ||
@@ -220,3 +228,3 @@ options.headers= options.headers || {}; | ||
if (method === 'POST' || method === 'PUT' || method === 'DELETE' || method === 'PATCH'){ | ||
options.headers['Content-Length'] = 0; | ||
options.headers[CONSTANTS.HEADER_CONTENT_LENGTH] = 0; | ||
} | ||
@@ -235,9 +243,9 @@ } else if (typeof args === 'object') { | ||
//always set Content-length header | ||
//set Content lentgh for some servers to work (nginx, apache) | ||
if (args.data !== undefined){ | ||
//always set Content-length header if not set previously | ||
//set Content lentgh for some servers to work (nginx, apache) | ||
if (args.data !== undefined && !options.headers.hasOwnProperty(CONSTANTS.HEADER_CONTENT_LENGTH)){ | ||
options.data = args.data; | ||
options.headers['Content-Length'] = Buffer.byteLength((typeof args.data === 'string' ? args.data:JSON.stringify(args.data)), 'utf8'); | ||
options.headers[CONSTANTS.HEADER_CONTENT_LENGTH] = Buffer.byteLength((typeof args.data === 'string' ? args.data:JSON.stringify(args.data)), 'utf8'); | ||
}else{ | ||
options.headers['Content-Length'] = 0; | ||
options.headers[CONSTANTS.HEADER_CONTENT_LENGTH] = 0; | ||
} | ||
@@ -276,2 +284,9 @@ // we have args, go and check if we have parameters | ||
} | ||
}, | ||
createHttpMethod:function(methodName){ | ||
return function(url, args, callback){ | ||
var clientRequest = new ClientRequest(); | ||
Util.connect(methodName.toUpperCase(), url, args, callback, clientRequest); | ||
return clientRequest; | ||
}; | ||
} | ||
@@ -306,33 +321,13 @@ }, | ||
this.get = function(url, args, callback){ | ||
var clientRequest = new ClientRequest(); | ||
Util.connect('GET', url, args, callback, clientRequest); | ||
return clientRequest; | ||
}; | ||
this.get = Util.createHttpMethod("get"); | ||
this.post = function(url, args, callback){ | ||
var clientRequest = new ClientRequest(); | ||
Util.connect('POST', url, args, callback, clientRequest); | ||
return clientRequest; | ||
}; | ||
this.post = Util.createHttpMethod("post"); | ||
this.put = Util.createHttpMethod("put"); | ||
this.put = function(url, args, callback){ | ||
var clientRequest = new ClientRequest(); | ||
Util.connect('PUT', url, args, callback, clientRequest); | ||
return clientRequest; | ||
}; | ||
this.delete = Util.createHttpMethod("delete"); | ||
this.delete = function(url, args, callback){ | ||
var clientRequest = new ClientRequest(); | ||
Util.connect('DELETE', url, args, callback, clientRequest); | ||
return clientRequest; | ||
}; | ||
this.patch = Util.createHttpMethod("patch"); | ||
this.patch = function(url, args, callback){ | ||
var clientRequest = new ClientRequest(); | ||
Util.connect('PATCH', url, args, callback, clientRequest); | ||
return clientRequest; | ||
}; | ||
this.registerMethod = function(name, url, method){ | ||
@@ -348,2 +343,6 @@ // create method in method registry with preconfigured REST invocation | ||
this.addCustomHttpMethod=function(methodName){ | ||
self[methodName.toLowerCase()] = Util.createHttpMethod(methodName); | ||
} | ||
// handle ConnectManager events | ||
@@ -362,3 +361,3 @@ connectManager.on('error',function(err){ | ||
var ConnectManager = function() { | ||
this.xmlctype = ["application/xml","application/xml;charset=utf-8"]; | ||
this.xmlctype = ["application/xml","application/xml;charset=utf-8","text/xml","text/xml;charset=utf-8"]; | ||
this.jsonctype = ["application/json","application/json;charset=utf-8"], | ||
@@ -370,3 +369,3 @@ this.isXML = function(content){ | ||
for (var i=0; i<this.xmlctype.length;i++){ | ||
result = this.xmlctype[i].toLowerCase() === content.toLowerCase(); | ||
result = this.xmlctype[i].trim().toLowerCase() === content.trim().toLowerCase(); | ||
if (result) break; | ||
@@ -382,3 +381,3 @@ } | ||
for (var i=0; i<this.jsonctype.length;i++){ | ||
result = this.jsonctype[i].toLowerCase() === content.toLowerCase(); | ||
result = this.jsonctype[i].trim().toLowerCase() === content.trim().toLowerCase(); | ||
if (result) break; | ||
@@ -443,5 +442,6 @@ } | ||
debug("response content is ",content); | ||
debug("response content is [",content,"]"); | ||
// XML data need to be parsed as JS object | ||
if (this.isXML(content)){ | ||
debug("detected XML response"); | ||
parseString(data.toString(), function (err, result) { | ||
@@ -451,2 +451,3 @@ callback(result, res); | ||
}else if (this.isJSON(content)){ | ||
debug("detected JSON response"); | ||
var jsonData, | ||
@@ -466,2 +467,3 @@ data = data.toString(); | ||
}else{ | ||
debug("detected unknown response"); | ||
callback(data, res); | ||
@@ -468,0 +470,0 @@ } |
@@ -5,3 +5,3 @@ { | ||
"description": "node API REST client", | ||
"version": "2.0.1", | ||
"version": "2.5.0", | ||
"repository": { | ||
@@ -14,3 +14,4 @@ "type":"git", | ||
"xml2js":">=0.2.4", | ||
"debug": "~2.2.0" | ||
"debug": "~2.2.0", | ||
"follow-redirects":">=1.2.0" | ||
}, | ||
@@ -17,0 +18,0 @@ "devDependencies": { |
# REST Client for Node.js | ||
[![npm version](https://badge.fury.io/js/node-rest-client.svg)](https://www.npmjs.com/package/node-rest-client) | ||
[![Build Status](https://travis-ci.org/olalonde/node-rest-client.svg?branch=master)](https://travis-ci.org/olalonde/node-rest-client) | ||
[![NPM](https://nodei.co/npm/node-rest-client.png?downloads=true)](https://nodei.co/npm/node-rest-client.png?downloads=true) | ||
**NOTE:** _Since version 0.8.0 node does not contain node-waf anymore. The node-zlib package which node-rest-client make use of, depends on node-waf.Fortunately since version 0.8.0 zlib is a core dependency of node, so since version 1.0 of node-rest-client the explicit dependency to "zlib" has been removed from package.json. therefore if you are using a version below 0.8.0 of node please use a versión below 1.0.0 of "node-rest-client". _ | ||
@@ -10,2 +14,3 @@ | ||
- Allows most common HTTP operations: GET, POST, PUT, DELETE, PATCH. | ||
- Allows creation of custom HTTP Methods (PURGE, etc.) | ||
- Direct or through proxy connection to remote API sites. | ||
@@ -17,2 +22,3 @@ - Register remote API operations as client own methods, simplifying reuse. | ||
- Added support for compressed responses: gzip and deflate | ||
- Added support for follow redirects thanks to great [follow-redirects](https://www.npmjs.com/package/follow-redirects) package | ||
@@ -157,4 +163,2 @@ | ||
### HTTP POST and PUT methods | ||
@@ -290,4 +294,20 @@ | ||
``` | ||
### Follows Redirect | ||
Node REST client follows redirects by default to a maximum of 21 redirects, but it's also possible to change follows redirect default config in each request done by the client | ||
```javascript | ||
var client = new Client(); | ||
// request and response additional configuration | ||
var args = { | ||
requestConfig: { | ||
followRedirects:true,//whether redirects should be followed(default,true) | ||
maxRedirects:10//set max redirects allowed (default:21) | ||
}, | ||
responseConfig: { | ||
timeout: 1000 //response timeout | ||
} | ||
}; | ||
``` | ||
### Connect through proxy | ||
@@ -294,0 +314,0 @@ |
@@ -0,0 +0,0 @@ {"catalog":{ |
@@ -0,0 +0,0 @@ var jasmine = require('jasmine-node'); |
@@ -0,0 +0,0 @@ var http = require('http'); |
@@ -0,0 +0,0 @@ var http = require('http'), |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
46154
11
735
483
2
3
+ Addedfollow-redirects@>=1.2.0
+ Addedfollow-redirects@1.15.9(transitive)