Simple Asynchronous HTTP Client
Reqwest is a browser-focused JavaScript library designed for making asynchronous HTTP requests, offering support for XMLHttpRequest, JSONP, CORS, and the CommonJS Promises/A specification. While it provided isomorphic capabilities allowing use in Node.js via the `xhr2` peer dependency, its primary intention and optimization were for client-side AJAX operations. The package is currently at version 2.0.5 and has not received any updates since August 2015. This effectively marks it as an abandoned project. Its Promise implementation adheres to an older specification (Promises/A), which may not be fully compatible with the ES2015+ Promises/A+ standard or modern `async/await` patterns. For contemporary JavaScript development, the native Fetch API or actively maintained alternatives like `axios` are generally preferred due to `reqwest`'s lack of ongoing support and potential compatibility issues with modern environments and security practices.
Common errors
-
TypeError: require is not a function
cause Attempting to import or use `reqwest` via CommonJS `require()` syntax within an ECMAScript Module (ESM) file or environment.fixEnsure your project is configured to use CommonJS modules for files that interact with `reqwest` (e.g., by ensuring 'type': 'module' is not present in the nearest `package.json`, or by using a build tool that correctly bundles CommonJS modules for ESM consumption). Alternatively, consider migrating to a modern, ESM-compatible HTTP client. -
ReferenceError: XMLHttpRequest is not defined
cause Using `reqwest` in a Node.js environment without the `xhr2` package installed or properly configured to polyfill the `XMLHttpRequest` object.fixInstall the `xhr2` package (`npm install xhr2`). Note that even with `xhr2`, `reqwest` is not recommended for Node.js, and this setup may still lead to other compatibility issues.
Warnings
- breaking The `reqwest` library is abandoned and has not received any updates since August 2015. It is likely incompatible with modern JavaScript runtimes, browsers, and security standards, and may contain unpatched vulnerabilities or unexpected behavior with newer language features.
- deprecated `reqwest` implements the older CommonJS Promises/A specification, which is not fully compatible with the ES2015+ Promises/A+ standard. This can lead to subtle bugs or integration issues when mixing `reqwest` with modern JavaScript's native Promises or `async/await` syntax.
- gotcha While `reqwest` claims isomorphism and can technically run in Node.js with the `xhr2` polyfill, its `README` explicitly discourages this for robust Node.js solutions, recommending `mikeal/request` (which is also now deprecated). It lacks native Node.js HTTP client features and integrations.
- gotcha There are no official or well-maintained community TypeScript type definitions for `reqwest`. Using this library in a TypeScript project will result in a lack of type safety and a poor developer experience, requiring manual type declarations or `@ts-ignore` directives.
Install
-
npm install reqwest -
yarn add reqwest -
pnpm add reqwest
Imports
- reqwest
import reqwest from 'reqwest'
const reqwest = require('reqwest')
Quickstart
const reqwest = require('reqwest');
// Example 1: Basic GET request with callback
reqwest('https://jsonplaceholder.typicode.com/posts/1', function (resp) {
console.log('GET with callback response:', resp);
});
// Example 2: POST request with JSON data and Promises
reqwest({
url: 'https://jsonplaceholder.typicode.com/posts',
method: 'post',
type: 'json',
contentType: 'application/json',
data: JSON.stringify({ title: 'foo', body: 'bar', userId: 1 }),
headers: {
'X-Custom-Header': 'MyValue'
}
})
.then(function (resp) {
console.log('POST with Promise success:', resp);
})
.fail(function (err, msg) {
console.error('POST with Promise error:', err, msg);
})
.always(function () {
console.log('POST request completed (always).');
});
// Example 3: JSONP request (assuming a JSONP endpoint like 'https://example.com/data.jsonp?callback=?')
// For demonstration, this won't run without a live JSONP service.
reqwest({
url: 'https://httpbin.org/jsonp?callback=?', // Using a placeholder for demonstration
type: 'jsonp',
jsonpCallback: 'callback',
success: function (resp) {
console.log('JSONP request successful:', resp);
},
error: function (err) {
console.error('JSONP request failed:', err);
}
});