REST Client for Node.js
Features
Allows connecting to any API REST and get results as js Object. The client has the following features:
- Transparent HTTP/HTTPS connection to remote API sites.
- Allows simple HTTP basic authentication.
- Allows most common HTTP operations: GET, POST, PUT, DELETE, PATCH or any other method through custom connect method
- Allows creation of custom HTTP Methods (PURGE, etc.)
- Direct or through proxy connection to remote API sites.
- Register remote API operations as own client methods, simplifying reuse.
- Dynamic path and query parameters and request headers.
- Improved Error handling mechanism (client or specific request)
- Added support for compressed responses: gzip and deflate
- Added support for follow redirects thanks to great follow-redirects package
- Added support for custom request serializers (json,xml and url-encoded included by default)
- Added support for custom response parsers (json and xml included by default)
Installation
$ npm install node-rest-client
Usages
Simple HTTP GET
Client has two ways to call a REST service: direct or using registered methods
var Client = Client; var client = ; // direct wayclient; // registering remote methodsclient; clientmethods;
HTTP POST
POST, PUT or PATCH method invocation are configured like GET calls with the difference that you have to set "Content-Type" header in args passed to client method invocation:
//Example POST method invocationvar Client = Client; var client = ; // set content-type header and data as json in args parametervar args = data: test: "hello" headers: "Content-Type": "application/json" ; client; // registering remote methodsclient; clientmethods;
If no "Content-Type" header is set as client arg POST,PUT and PATCH methods will not work properly.
Passing args to registered methods
You can pass diferents args to registered methods, simplifying reuse: path replace parameters, query parameters, custom headers
var Client = Client; // direct wayvar client = ; 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" // this is serialized as URL parameters headers: "test-header": "client-api" // request headers; client; // registering remote methodsclient; /* this would construct the following URL before invocation * * http://remote.site/rest/json/120/method?arg1=hello&arg2=world * */clientmethods;
You can even use path placeholders in query string in direct connection:
var Client = Client; // direct wayvar client = ; var args = path: "id": 120 "arg1": "hello" "arg2": "world" headers: "test-header": "client-api" ; client;
HTTP POST and PUT methods
To send data to remote site using POST or PUT methods, just add a data attribute to args object:
var Client = Client; // direct wayvar client = ; 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; // registering remote methodsclient; clientmethods; // posted data can be js objectvar args_js = path: "id": 120 parameters: arg1: "hello" arg2: "world" headers: "test-header": "client-api" data: "arg1": "hello" "arg2": 123 ; clientmethods;
Request/Response configuration
It's also possible to configure each request and response, passing its configuration as an additional argument in method call.
var client = ; // request and response additional configurationvar 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;
If you want to handle timeout events both in the request and in the response just add a new "requestTimeout" or "responseTimeout" event handler to clientRequest returned by method call.
var client = ; // request and response additional configurationvar 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; req; req; //it's usefull to handle request errors to avoid, for example, socket hang up errors on request timeoutsreq;
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
var client = ; // request and response additional configurationvar args = requestConfig: followRedirects:true//whether redirects should be followed(default,true) maxRedirects:10//set max redirects allowed (default:21) responseConfig: timeout: 1000 //response timeout ;
Response Parsers
You can add your own response parsers to client, as many as you want. There are 2 parser types:
-
Regular parser: First ones to analyze responses. When a response arrives it will pass through all regular parsers, first parser whose
match
method return true will be the one to process the response. there can be as many regular parsers as you need. you can delete and replace regular parsers when it'll be needed. -
Default parser: When no regular parser has been able to process the response, default parser will process it, so it's guaranteed that every response is processed. There can be only one default parser and cannot be deleted but it can be replaced adding a parser with
isDefault
attribute to true.
Each parser - regular or default- needs to follow some conventions:
-
Must be and object
-
Must have the following attributes:
-
name
: Used to identify parser in parsers registry -
isDefault
: Used to identify parser as regular parser or default parser. Default parser is applied when client cannot find any regular parser that match to received response
-
-
Must have the following methods:
-
match(response)
: used to find which parser should be used with a response. First parser found will be the one to be used. Its arguments are:response
:http.ServerResponse
: you can use any argument available in node ServerResponse, for exampleheaders
-
parse(byteBuffer,nrcEventEmitter,parsedCallback)
: this method is where response body should be parsed and passed to client request callback. Its arguments are:byteBuffer
:Buffer
: Raw response body that should be parsed as js object or whatever you neednrcEventEmitter
:client event emitter
: useful to dispatch events during parsing process, for example error eventsparsedCallback
:function(parsedData)
: this callback should be invoked when parsing process has finished to pass parsed data to request callback.
-
Of course any other method or attribute needed for parsing process can be added to parser.
// no "isDefault" attribute defined var invalid = "name":"invalid-parser" {...} {...} ; var validParser = "name":"valid-parser" "isDefault": false {...} {...} // of course any other args or methods can be added to parser "otherAttr":"my value" {...} ; { thisname: name thisisDefault: false this{...}; thisparse:{...}; } var instanceParser = "instance-parser"; //valid parser complete example clientparsers
By default and to maintain backward compatibility, client comes with 2 regular parsers and 1 default parser:
- JSON parser: it's named 'JSON' in parsers registry and processes responses to js object. As in previous versions you can change content-types used to match responses by adding a "mimetypes" attribute to client options.
var options = mimetypes: json: "application/json" "application/my-custom-content-type-for-json;charset=utf-8" ; var client = options;
- XML parser: it's named 'XML' in parsers registry and processes responses returned as XML documents to js object. As in previous versions you can change content-types used to match responses by adding a "mimetypes" attribute to client options.
var options = mimetypes: xml: "application/xml" "application/my-custom-content-type-for-xml" ; var client = options;
Additionally in this parser there's an attribute "options" where you can customize xml2js parser options. Please refer to xml2js package for valid parser options.
var client = ; clientparsersoptions= "explicitArray":false "ignoreAttrs":true;
- Default Parser: return responses as is, without any adittional processing.
Parser Management
Client can manage parsers through the following parsers namespace methods:
-
add(parser)
: add a regular or default parser (depending on isDefault attribute value) to parsers registry. If you add a regular parser with the same name as an existing one, it will be overwrittenparser
: valid parser object. If invalid parser is added an 'error' event is dispatched by client.
-
remove(parserName)
: removes a parser from parsers registry. If not parser found an 'error' event is dispatched by client.parserName
: valid parser name previously added.
-
find(parserName)
: find and return a parser searched by its name. If not parser found an 'error' event is dispatched by client.parserName
: valid parser name previously added.
-
getAll()
: return a collection of current regular parsers. -
getDefault()
: return the default parser used to process responses that doesn't match with any regular parser. -
clean()
: clean regular parser registry. default parser is not afected by this method.
var client = ; clientparsers; var parser = clientparsers; var defaultParser = clientparsers; var regularParsers = clientparsers; clientparsers;
Request Serializers
You can add your own request serializers to client, as many as you want. There are 2 serializer types:
-
Regular serializer: First ones to analyze requests. When a request is sent it will pass through all regular serializers, first serializer whose
match
method return true will be the one to process the request. there can be as many regular serializers as you need. you can delete and replace regular serializers when it'll be needed. -
Default serializer: When no regular serializer has been able to process the request, default serializer will process it, so it's guaranteed that every request is processed. There can be only one default serializer and cannot be deleted but it can be replaced adding a serializer with
isDefault
attribute to true.
Each serializer - regular or default- needs to follow some conventions:
-
Must be and object
-
Must have the following attributes:
-
name
: Used to identify serializer in serializers registry -
isDefault
: Used to identify serializer as regular serializer or default serializer. Default serializer is applied when client cannot find any regular serializer that match to sent request
-
-
Must have the following methods:
-
match(request)
: used to find which serializer should be used with a request. First serializer found will be the one to be used. Its arguments are:request
:options passed to http.ClientRequest
: any option passed to a request through client options or request args, for exampleheaders
-
serialize(data,nrcEventEmitter,serializedCallback)
: this method is where request body should be serialized before passing to client request callback. Its arguments are:-
data
:args data attribute
: Raw request body as is declared in args request attribute that should be serialized. -
nrcEventEmitter
:client event emitter
: useful to dispatch events during serialization process, for example error events -
serializedCallback
:function(serializedData)
: this callback should be invoked when serialization process has finished to pass serialized data to request callback.
-
-
Of course any other method or attribute needed for serialization process can be added to serializer.
// no "isDefault" attribute defined var invalid = "name":"invalid-serializer" {...} {...} ; var validserializer = "name":"valid-serializer" "isDefault": false {...} {...} // of course any other args or methods can be added to serializer "otherAttr":"my value" {...} ; { thisname: name thisisDefault: false this{...}; thisserialize:{...}; } var instanceserializer = "instance-serializer"; // valid serializer complete example clientserializers
By default client comes with 3 regular serializers and 1 default serializer:
-
JSON serializer: it's named 'JSON' in serializers registry and serialize js objects to its JSON string representation. It will match any request sent exactly with the following content types: "application/json","application/json;charset=utf-8"
-
XML serializer: it's named 'XML' in serializers registry and serialize js objects to its XML string representation. It will match any request sent exactly with the following content types: "application/xml","application/xml;charset=utf-8","text/xml","text/xml;charset=utf-8"
Additionally in this parser there's an attribute "options" where you can customize xml2js serializer options. Please refer to xml2js package for valid builder options.
var client = ; clientserializersoptions="renderOpts":"pretty": true ;
-
URL ENCODE serializer: it's named 'FORM-ENCODED' in serializers registry and serialize js objects to its FORM ENCODED string representation. It will match any request sent exactly with the following content types: "application/x-www-form-urlencoded","multipart/form-data","text/plain"
-
Default serializer: serialize request to its string representation, applying toString() method to data parameter.
serializer Management
Client can manage serializers through the following serializers namespace methods:
-
add(serializer)
: add a regular or default serializer (depending on isDefault attribute value) to serializers registry.If you add a regular serializer with the same name as an existing one, it will be overwrittenserializer
: valid serializer object. If invalid serializer is added an 'error' event is dispatched by client.
-
remove(serializerName)
: removes a serializer from serializers registry. If not serializer found an 'error' event is dispatched by client.serializerName
: valid serializer name previously added.
-
find(serializerName)
: find and return a serializer searched by its name. If not serializer found an 'error' event is dispatched by client.serializerName
: valid serializer name previously added.
-
getAll()
: return a collection of current regular serializers. -
getDefault()
: return the default serializer used to process requests that doesn't match with any regular serializer. -
clean()
: clean regular serializer registry. default serializer is not afected by this method.
var client = ; clientserializers
Connect through proxy
Just pass proxy configuration as option to client.
var Client = Client; // configure proxyvar options_proxy = proxy: host: "proxy.foo.com" port: 8080 user: "proxyuser" password: "123" tunnel: true ; var client = options_proxy;
client has 2 ways to connect to target site through a proxy server: tunnel or direct request, the first one is the default option so if you want to use direct request you must set tunnel off.
var Client = Client; // configure proxyvar options_proxy = proxy: host: "proxy.foo.com" port: 8080 user: "proxyuser" password: "123" tunnel: false // use direct request to proxy ; var client = options_proxy;
Basic HTTP auth
Just pass username and password or just username, if no password is required by remote site, as option to client. Every request done with the client will pass username and password or just username if no password is required as basic authorization header.
var Client = Client; // configure basic http auth for every requestvar options_auth = user: "admin" password: "123" ; var client = options_auth;
Options parameters
You can pass the following args when creating a new client:
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 // aditional connection options passed to node http.request y https.request methods // (ie: options to connect to IIS with SSL) connection: secureOptions: constantsSSL_OP_NO_TLSv1_2 ciphers: 'ECDHE-RSA-AES256-SHA:AES256-SHA:RC4-SHA:RC4:HIGH:!MD5:!aNULL:!EDH:!AESGCM' honorCipherOrder: true // will replace content-types used to match responses in JSON and XML parsers 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 responseConfig: timeout: 1000 //response timeout ;
Note that requestConfig and responseConfig options if set on client instantiation apply to all of its requests/responses and is only overriden by request or reponse configs passed as args in method calls.
Managing Requests
Each REST method invocation returns a request object with specific request options and error, requestTimeout and responseTimeout event handlers.
var Client = Client; var client = ; var args = requesConfig: timeout: 1000 responseConfig: timeout: 2000 ; // direct wayvar req1 = client; // view req1 options console; req1; req1; // registering remote methodsclient; var req2 = clientmethods; // handling specific req2 errorsreq2;
Error Handling
Now you can handle error events in two places: on client or on each request.
var client = options_auth; // handling request error eventsclient; // handling client error eventsclient;
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". _