Node.js HTTP/HTTPS client for JSON APIs. Supports GET, PUT, POST, DELETE and PATCH methods.
npm install('remote-json')
var remote = require('remote-json');
remote('http://echo.jsontest.com/key/value/name/Bob')
.get(function (err, res, body) {
console.log(res.statusCode); // 200
console.log(body); // {"name": "Bob", "key": "value"}
});Returns a new instance of Remote object with JSON API's methods.
url- A full URL string.opt- A HTTP options object.
Returns a function for GET method.
path- Optional. A path which will be added to the URL.data- Optional. A data wich will be send with request.callback- Callback function.
Many paths at one remote:
var remote = remote('http://json.api');
remote.get('/users', callbackUsers);
remote.get('/books', callbackBooks);Send data on request:
remote.get('/users', {id: 123}, callback);
remote.get('/users', {id: 78}, callback);Usage for these methods is equal to the one of the .get method.
According to the standard, the MIME media type for JSON text is application/json. This module checks if the type is valid. You may enable any other type to pass the validation, e.g.:
remote.contentTypes['text/javascript'] = true;Pass it to the constructor:
remote('http://json.api', {
headers: {
Cookie: 'Your cookie'
}
});This module ALWAYS tries to parse the response body as JSON regardless the response status code. You may need to take of it since some API's may return no content as a valid response, e.g. 204 status code:
remote('http://json.api').post({}, function (err, res, body) {
if (!err || 204 === res.statusCode) {
// ok
}
});This client doesn't follow redirects. Use the follow-redirects module for this:
remote.http = require('follow-redirects').http;
remote.https = require('follow-redirects').https;npm test
The tests use online services:
They may be down for the momemnt you test this module.