jtwb / request-promise

Promise-based Wrapper for Simple HTTP Requests built on top of Bluebird and Request

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Request-Promise

A Promises/A XHR wrapper for Bluebird and Request

Bluebird and Request are pretty awesome, but I found myself using the same design pattern. This is a simple wrapper that takes in a request options object (or URI string), and returns a chainable promise. By default, http response codes other than 200 and 201 will cause the promise to be rejected. This can be over-ridden by setting options.simple to false.

Note: As of version 0.1, reject now passes an object containing the following:

    reject({
                    error: body,
                    options: c,
                    response: response,
                    statusCode: response.statusCode
                });

Installation

npm install request-promise

Examples

var rp = require('request-promise');

rp('http://www.google.com')
    .then(console.dir)
    .catch(console.error);

//'GET's and displays google.com

var options = {
    uri : 'http://posttestserver.com/post.php',
    method : 'POST'
}; 

rp(options)
    .then(console.dir)
    .catch(console.error);

//displays response from server after post

options.transform = function (data) { return data.length ;};

rp(options)
    .then(console.dir)
    .catch(console.error);

//transform is called just before promise is fulfilled
//displays length of response from server after post

MIT Licenced

About

Promise-based Wrapper for Simple HTTP Requests built on top of Bluebird and Request


Languages

Language:JavaScript 100.0%