# 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)
## 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](https://www.npmjs.com/package/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
```javascript
var Client = require('node-rest-client').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);
});
// registering remote methods
client.registerMethod("jsonMethod", "http://remote.site/rest/json/method", "GET");
client.methods.jsonMethod(function (data, response) {
// parsed response body as js object
console.log(data);
// raw response
console.log(response);
});
```
### 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:
```javascript
//Example POST method invocation
var Client = require('node-rest-client').Client;
var client = new Client();
// set content-type header and data as json in args parameter
var args = {
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
console.log(data);
// raw response
console.log(response);
});
// registering remote methods
client.registerMethod("postMethod", "http://remote.site/rest/json/method", "POST");
client.methods.postMethod(args, function (data, response) {
// parsed response body as js object
console.log(data);
// raw response
console.log(response);
});
```
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
```javascript
var Client = require('node-rest-client').Client;
// direct way
var client = new 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.get("http://remote.site/rest/json/${id}/method", args,
function (data, response) {
// parsed response body as js object
console.log(data);
// raw response
console.log(response);
});
// registering remote methods
client.registerMethod("jsonMethod", "http://remote.site/rest/json/${id}/method", "GET");
/* this would construct the following URL before invocation
*
* http://remote.site/rest/json/120/method?arg1=hello&arg2=world
*
*/
client.methods.jsonMethod(args, function (data, response) {
// parsed response body as js object
console.log(data);
// raw response
console.log(response);
});
```
You can even use path placeholders in query string in direct connection:
```javascript
var Client = require('node-rest-client').Client;
// direct way
var client = new Client();
var args = {
path: { "id": 120, "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);
});
```
### HTTP POST and PUT methods
To send data to remote site using POST or PUT methods, just add a data attribute to args object:
```javascript
var Client = require('node-rest-client').Client;
// direct way
var client = new Client();
var args = {
path: { "id": 120 },
parameters: { arg1: "hello", arg2: "world" },
headers: { "test-header": "client-api" },
data: "
helloworld"
};
client.post("http://remote.site/rest/xml/${id}/method", args, function (data, response) {
// parsed response body as js object
console.log(data);
// raw response
console.log(response);
});
// registering remote methods
client.registerMethod("xmlMethod", "http://remote.site/rest/xml/${id}/method", "POST");
client.methods.xmlMethod(args, function (data, response) {
// parsed response body as js object
console.log(data);
// raw response
console.log(response);
});
// posted data can be js object
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) {
// parsed response body as js object
console.log(data);
// raw response
console.log(response);
});
```
### Request/Response configuration
It's also possible to configure each request and response, passing its configuration as an
additional argument in method call.
```javascript
var client = new Client();
// request and response additional configuration
var args = {
path: { "id": 120 },
parameters: { arg1: "hello", arg2: "world" },
headers: { "test-header": "client-api" },
data: "
helloworld",
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", args, function (data, response) {
// parsed response body as js object
console.log(data);
// raw response
console.log(response);
});
```
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.
```javascript
var client = new Client();
// request and response additional configuration
var args = {
path: { "id": 120 },
parameters: { arg1: "hello", arg2: "world" },
headers: { "test-header": "client-api" },
data: "
helloworld",
requestConfig: {
timeout: 1000, //request timeout in milliseconds
noDelay: true, //Enable/