Thin wrapper around request module to provide simple JSON API with q promises. All methods return promise resolving to json object.
npm install request-promise-json --save
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
});
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);
});