1.0.4 • Published 9 years ago

request-promise-json v1.0.4

Weekly downloads
4,319
License
ISC
Repository
github
Last release
9 years ago

request-promise-json

Thin wrapper around request module to provide simple JSON API with q promises. All methods return promise resolving to json object.

Install

npm install request-promise-json --save

Usage:

var http = require('request-promise-json');

http(method, url, [json]).then(function(resultJson){
    console.log(resultJson.id);
});

// or use shortcuts:
http.get(url);
http.post(url, json);
http.put(url, json);

// or provide options object directy to 'request' library 
// (see docs for 'request' module https://www.npmjs.com/package/request):
http.request({
    method: 'POST',
    url: url,
    body: json
});

Error Handling:

If request fails with an error, promise will be rejected with that error.

If response status code is >= 400, reject Error will have following properties:

  • statusCode
  • request - request options (method, url, body)
  • response - response body
// Example of error handling:
http.get(url).fail(function(reason){
    if(reason.statusCode === 400)
        console.log('Bad request', reason.request);
    else
        console.log('failed with error', reason);
});