📜  @apify http-request (1)

📅  最后修改于: 2023-12-03 15:13:11.576000             🧑  作者: Mango

Introducing '@apify/http-request'

Overview

@apify/http-request is a Node.js package that provides an easy-to-use promise-based HTTP client with support for retries and timeouts.

Features
  • Simple and easy to use API
  • Promise-based, no callbacks
  • Automatically handles HTTP errors and retries failed requests
  • Optional timeout settings
  • Returns response body as a Buffer or parsed JSON object
  • Configurable amount of retries and retry delays
Installation

You can install @apify/http-request using NPM:

npm install @apify/http-request
Usage

To use @apify/http-request, simply import the package and call the request() function with the desired URL and options:

const { request } = require('@apify/http-request');

const options = {
  method: 'GET',
  headers: {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36',
  },
};

(async () => {
  try {
    const response = await request('https://example.com', options);

    console.log(`Response status code: ${response.statusCode}`);
    console.log(`Response headers: ${JSON.stringify(response.headers)}`);
    console.log(`Response body: ${response.body.toString()}`);
  } catch (err) {
    console.error(err);
  }
})();
Options

url

(required) The URL to make the HTTP request to.

method

(optional) The HTTP method to use. Defaults to 'GET'.

headers

(optional) An object of HTTP request headers to send.

payload

(optional) A string, Buffer or object to send as the request body.

contentType

(optional) The content type of the request body.

timeout

(optional) The timeout in milliseconds. Defaults to no timeout.

maxRetries

(optional) The maximum amount of retries before failing the request. Defaults to 3.

retryDelay

(optional) The time in milliseconds to wait before retrying the request. Defaults to 1000.

Conclusion

@apify/http-request is a simple and powerful HTTP client with support for retries and timeouts. It provides a clean API that is easy to use and will handle HTTP requests in a robust and reliable way.