HTTP as Promised — Promisified HTTP client.

Using Bluebird and Create Error to make Request easier to use. The most notible difference between this library and simply "promisifying" the request module is that this library will automatically reject the promise with an HTTPError
if the response status code is an HTTP error status code (e.g. response.statusCode >= 400
).
Super simple to use
HTTP as Promised is designed to be the simplest way possible to make http calls. It supports HTTPS and follows redirects by default.
var request = require('http-as-promised');
request('http://www.google.com').spread(function(response, body){
console.log(body)
}).catch(function (error){
console.error(error)
})
HTTP Errors
HTTP as Promised exposes a custom HTTPError
constructor which is extended from the global Error
constructor. The HTTPError
constructor also exposes more specific types of HTTPError
constructors both for ranges/types of HTTP Errors (4xx/client and 5xx/server) as well as status-code-specific HTTP errors (404, 418, 509, etc.). When instanciated, each of these constructors will be a fully-fledged instanceof Error
with stack traces and everything. In addition to the message
and name
properties, instances of HTTPError
will also include the following properties:
statusCode
(e.g. 404
)title
(e.g. "Not Found"
)summary
(e.g. "requested resource could not be found"
)type
(e.g. "ClientError"
)range
(e.g. "4xx"
)
Catching HTTP Errors
Since we're using Bluebird to construct our promises, catching and handling specific HTTP Errors is a breeze.
var request = require('http-as-promised');
request('http://www.google.com')
.catch(request.error[404], function(e){
})
.catch(request.error.client, function(e){
})
.catch(request.error['4xx'], function(e){
})
.catch(request.error, function(e){
})
Consistency with Request
HTTP as Promised supports all the same options you'd pass to Request as well as all of Request's convenience methods.