The Big Picture - screenly/anthias - Request

Request is a popular and simplified HTTP client for making requests in Node.js. It offers various features such as streaming, promises, forms, HTTP authentication, custom headers, OAuth signing, proxies, Unix Domain Sockets, and TLS/SSL protocol support. In this guide, we’ll explain why you might want to use Request and provide an example of how to use it.

Why Use Request?

Request is a popular choice for making HTTP requests in Node.js due to its simplicity and ease of use. It offers various features that make it a versatile tool for handling HTTP requests. Some of these features include:

  • Streaming: Request supports streaming any response to a file stream or streaming a file to a PUT or POST request.
  • Promises & Async/Await: Request supports both streaming and callback interfaces natively. However, if you prefer to work with Promises or async/await, you can use alternative interface wrappers like request-promise, request-promise-native, or request-promise-any.
  • Forms: Request supports application/x-www-form-urlencoded and multipart/form-data form uploads.
  • HTTP Authentication: Request supports HTTP Basic and Digest authentication.
  • Custom HTTP Headers: You can set custom HTTP headers in the options object.
  • OAuth Signing: Request supports OAuth 1.0 signing using HMAC-SHA1.
  • Proxies: Request supports making requests through HTTP and HTTPS proxies.
  • Unix Domain Sockets: Request supports making requests to Unix Domain Sockets.
  • TLS/SSL Protocol: Request supports TLS/SSL protocol options such as cert, key, and passphrase.
  • Support for HAR 1.2: Request supports the HAR 1.2 format for storing and transferring HTTP archives.

Installing Request

To start using Request in your project, first, install it by running the following command in your terminal:

npm i request

As of now, there are over 56,000 other projects in the npm registry using Request.

Example Usage

Here’s a simple example of making a GET request using Request:

const request = require('request');

request('http://www.google.com', (error, response, body) => {
if (error) {
console.error('Error:', error);
} else {
console.log('Status code:', response.statusCode);
console.log('Body:', body);
}
});

In this example, we import the Request module and make a GET request to Google’s homepage. We provide a callback function that will be executed when the response is received. The function takes three arguments: error, response, and body. If there’s an error, we print it to the console. Otherwise, we print the status code and the body of the response.

Table of Contents

  1. Streaming
  2. Promises & Async/Await
  3. Forms
  4. HTTP Authentication
  5. Custom HTTP Headers
  6. OAuth Signing
  7. Proxies
  8. Unix Domain Sockets
  9. TLS/SSL Protocol
  10. Support for HAR 1.2

Streaming

Request supports streaming any response to a file stream or streaming a file to a PUT or POST request.

Promises & Async/Await

Request supports both streaming and callback interfaces natively. However, if you prefer to work with Promises or async/await, you can use alternative interface wrappers like request-promise, request-promise-native, or request-promise-any.

Forms

Request supports application/x-www-form-urlencoded and multipart/form-data form uploads.

HTTP Authentication

Request supports HTTP Basic and Digest authentication.

Custom HTTP Headers

You can set custom HTTP headers in the options object.

OAuth Signing

Request supports OAuth 1.0 signing using HMAC-SHA1.

Proxies

Request supports making requests through HTTP and HTTPS proxies.

Unix Domain Sockets

Request supports making requests to Unix Domain Sockets.

TLS/SSL Protocol

Request supports TLS/SSL protocol options such as cert, key, and passphrase.

Support for HAR 1.2

Request supports the HAR 1.2 format for storing and transferring HTTP archives.

For more information on each of these topics, refer to the Request documentation.

Additional Resources

Conclusion

Request is a powerful and versatile tool for making HTTP requests in Node.js. Its features, such as streaming, promises, forms, HTTP authentication, custom headers, OAuth signing, proxies, Unix Domain Sockets, and TLS/SSL protocol support, make it a popular choice for developers. By following the example usage and the table of contents provided in this guide, you’ll be well on your way to using Request effectively in your projects. For more information, refer to the Request documentation and the additional resources listed above.