What is request-promise?
The request-promise npm package is a popular HTTP client for making HTTP requests from Node.js. It is built on top of the 'request' package, providing a simplified way to make HTTP calls and process responses using promises instead of callbacks.
What are request-promise's main functionalities?
Simple HTTP GET requests
This feature allows you to perform simple HTTP GET requests and process the response as a string.
const rp = require('request-promise');
rp('http://example.com')
.then(function (htmlString) {
// Process html...
})
.catch(function (err) {
// Handle error...
});
HTTP requests with options
This feature allows you to perform HTTP requests with a full set of options, including query strings, headers, and automatic JSON parsing.
const rp = require('request-promise');
const options = {
uri: 'http://api.example.com',
qs: {
access_token: 'my-token' // -> uri + '?access_token=my-token'
},
headers: {
'User-Agent': 'Request-Promise'
},
json: true // Automatically parses the JSON string in the response
};
rp(options)
.then(function (repos) {
// Process repos...
})
.catch(function (err) {
// Handle error...
});
POST requests with form data
This feature allows you to perform HTTP POST requests with form data.
const rp = require('request-promise');
const options = {
method: 'POST',
uri: 'http://api.example.com/form',
form: {
key: 'value',
another_key: 'another_value'
},
headers: {
/* 'content-type': 'application/x-www-form-urlencoded' */ // Is set automatically
}
};
rp(options)
.then(function (parsedBody) {
// POST succeeded...
})
.catch(function (err) {
// POST failed...
});
Other packages similar to request-promise
axios
Axios is a promise-based HTTP client for the browser and Node.js. It provides an easy-to-use API for making HTTP requests and can be used in both the front-end and back-end. It supports interceptors, request and response transformations, and automatic JSON data transformation, making it more feature-rich compared to request-promise.
got
Got is a human-friendly and powerful HTTP request library for Node.js. It is designed to be a simpler and more lightweight alternative to request-promise, with support for streams and promises. Got also provides a more modern API with built-in support for JSON and better error handling.
node-fetch
node-fetch is a light-weight module that brings the Fetch API to Node.js. It is a minimalistic and straightforward API that closely resembles the browser's fetch API, making it ideal for developers who prefer the fetch syntax and are looking for a simple way to make HTTP requests in Node.js.
superagent
Superagent is a small progressive client-side HTTP request library. It has a flexible and chainable API that allows for easy construction of complex queries. It is often compared to request-promise for its simplicity and ease of use but is more focused on being used in web browsers, although it works in Node.js as well.
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
.
Installation
npm install request-promise
Examples
var rp = require('request-promise');
rp('http://www.google.com')
.then(console.dir)
.catch(console.error);
var options = {
uri : 'http://posttestserver.com/post.php',
method : 'POST'
};
rp(options)
.then(console.dir)
.catch(console.error);
options.transform = function (data) { return data.length ;};
rp(options)
.then(console.dir)
.catch(console.error);
MIT Licenced