Postman Request Client
Postman-request is a maintained fork of the popular, now-abandoned `request` module, specifically developed and used internally by Postman Runtime. Currently at version 2.88.1-postman.48, it does not follow a typical open-source release cadence but is updated as needed for Postman's internal applications. This package provides a simplified API for making HTTP requests, addressing numerous long-standing bugs and adding specific features that were never incorporated into the original `request` project. Key differentiators include fixes for old-style deflate responses, correct URL parameter encoding, enhanced redirect behaviors (especially for 307/308 with Host headers), `content-length` for streaming, improved form-data exception handling, and features like retaining authorization headers on cross-domain redirects and support for extending root CA certificates. It aims to offer a stable and patched alternative for projects still reliant on the `request` API.
Common errors
-
Error: self signed certificate in certificate chain
cause The server's SSL certificate is self-signed or not trusted by the client's CA store.fixEnsure your system's root CA certificates are up-to-date. For private/self-signed CAs, pass the certificate authority via the `ca` option in the request configuration. Avoid `strictSSL: false` for security. -
TypeError: request is not a function
cause Incorrect import statement, often when trying to use ESM `import` syntax with a CommonJS module, or destructuring incorrectly.fixUse `const request = require('postman-request');` for CommonJS environments, which is the module's primary export pattern. -
Callback was already called.
cause A callback function was invoked multiple times, usually due to an internal error handling path or a malformed server response triggering a secondary error after the initial callback.fixEnsure your callback function only handles a single invocation. While `postman-request` aims to fix common `request` issues, robust error handling with flags can prevent double invocation: `let called = false; if (!called) { called = true; /* ...handle response... */ }`
Warnings
- breaking Original 'request' module is abandoned; 'postman-request' is a fork with bug fixes. While largely compatible, subtle behavioral differences might affect existing code.
- gotcha Legacy API and CommonJS-first design. The underlying `request` API is considered legacy compared to `node-fetch` or `axios`.
- gotcha Stream error handling requires specific placement. When chaining streams, an `error` event listener must be attached *before* piping.
- gotcha Not a general-purpose HTTP client for new projects. While actively maintained by Postman, its primary purpose is internal Postman Runtime needs.
- gotcha SSL/TLS certificate validation issues can occur. Misconfigured environments or self-signed certificates might lead to errors like 'self signed certificate'.
Install
-
npm install postman-request -
yarn add postman-request -
pnpm add postman-request
Imports
- request
import request from 'postman-request';
const request = require('postman-request'); - request.post
import { post } from 'postman-request';const request = require('postman-request'); request.post('http://example.com/upload', { form: { key: 'value' } }, (error, response, body) => { /* ... */ }); - request.defaults
const defaultRequest = require('postman-request').defaults;const request = require('postman-request'); const defaultRequest = request.defaults({ headers: { 'User-Agent': 'my-app' } });
Quickstart
const request = require('postman-request');
request('http://www.google.com', function (error, response, body) {
if (error) {
console.error('Error:', error);
return;
}
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log('body length:', body ? body.length : 0); // Print the length of the Google homepage HTML
});