Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

request-promise-json

marushkevych354ISC1.0.4

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

http, client, request, json, q, promise

readme

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