Skip to content

Simple JSON API with 'q' promises on top of amazing 'request' module.

Notifications You must be signed in to change notification settings

marushkevych/request-promise-json

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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);
});

About

Simple JSON API with 'q' promises on top of amazing 'request' module.

Resources

Stars

Watchers

Forks

Packages

No packages published