@apify/http-request

中文文档
2.1.2发布于 2021/03/02ISCCommonJS近 30 天下载 6109

Wrapper of got package used across Apify

原始使用文档
文档目录7 节
  1. Example Usage
  2. Get decoded and decompressed response
  3. Get JSON from API
  4. Stream the response
  5. API Documentation
  6. httpRequest([options])
  7. Response

HTTP request library for Node.js

Build Status

Sends a HTTP request and returns the response. This package is used it Apify SDK instead of the old request

NPM package. Implements a tunnel agent error fix, better proxy agents and supports brotli compression. Fixed deflate compression.

The function has similar functionality and has nearly the same options as the got NPM package, but it brings several additional improvements and fixes:

  • It support not only Gzip compression, but also Brotli and Deflate. To activate this feature, simply add Accept-Encoding: gzip, deflate, br to options.headers (or a combination) and set options.useBrotli to true.
  • Enables abortion of the request based on the response headers, before the data is downloaded. See options.abortFunction parameter.
  • Tunnel agent error fix
  • Fixed old deflate compression.
  • Brotli support for Node.js <v12

Example Usage

Get decoded and decompressed response

   const httpRequest = require("@apify/http-request");

   const {headers, body, statusCode} =  await httpRequest({ url: 'https://api.apify.com/v2/browser-info'});

Get JSON from API

    const httpRequest = require("@apify/http-request");

    const {headers, body, statusCode} =  await httpRequest({ url: 'https://api.apify.com/v2/browser-info', json: true });

Stream the response

     const httpRequest = require("@apify/http-request");

     const responseStream =  await httpRequest({ url: 'https://apify.com', stream: true});

API Documentation

It's a GET request by default, but can be changed by using different methods or via options.method.

httpRequest([options])

Returns a Promise for a response object or a stream if options.stream is set to true.

options.url

URL of the target endpoint. Supports both HTTP and HTTPS schemes.

options.method

HTTP method. Default value is GET.

options.headers

HTTP headers. Default value is {}.

Note that the function generates several headers itself, unless they are defined in the headers parameter, in which case the function leaves them untouched.

  • For example, even if you define { 'Content-Length': null }, the function doesn't define the 'Content-Length' header and the request will not contain it (due to the null value).

options.payload

HTTP payload for PATCH, POST and PUT requests. Must be a Buffer or String.

options.followRedirect

Follow HTTP 3xx responses as redirects (default: true).

options.maxRedirects

The maximum number of redirects to follow. Default value is 20

options.timeoutSecs

Integer containing the number of seconds to wait for a server to send response headers (and start the response body) before aborting the request. Note that if the underlying TCP connection cannot be established, the OS-wide TCP connection timeout will overrule the timeout option (the default in Linux can be anywhere from 20-120 seconds).

Default value is 30.

options.proxyUrl

An HTTP proxy to be used. Supports proxy authentication with Basic Auth.

options.ignoreSslErrors

If false, requires SSL/TLS certificates to be valid

Default value is false

options.abortFunction

A function that determines whether the request should be aborted. It is called when the server responds with the HTTP headers, but before the actual data is downloaded. class and it should return true if request should be aborted, or false otherwise. You can also throw custom error inside the options.abortFunction. In this case, httpRequest aborts the request and throws your custom error.

Default value is null.

options.throwOnHttpErrors

If set to true function throws and error on 4XX and 5XX response codes.

Default value is false.

options.decodeBody

If set to true decoded body is returned. Cannot be set to false if the [options.parsedBody] is true.

Default value is true

options.json

If set to true parsed body is returned. And content-type header is set to application/json It won't work if you have the options.stream set to true.

Default value is false.

options.stream

If set to true decompressed stream is returned.

Default value is false

options.useBrotli

If set to true Brotli decompression is enabled. Brotli has a Node.js native support from V10.16.0. If you use older version you must have the peer dependency iltorb installed.

Default value is false

Response

Promise<object> - The response object will typically be a

For more information please see original npm package - got.

版本与发布记录

@apify/http-request 共发布 22 个版本,首次发布于 2019/07/19;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.1.2latest2021/03/025 年前
2.1.12020/11/175 年前
2.1.02020/06/076 年前
2.0.12020/06/056 年前
2.0.02020/05/226 年前
1.2.02020/04/256 年前

关于 @apify/http-request 的常见问题

@apify/http-request 是什么?

Wrapper of got package used across Apify

如何安装 @apify/http-request?

在项目目录运行 npm install @apify/http-request,或使用 pnpm add / yarn add / bun add。

@apify/http-request 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/apify__http-request 是否存在。

@apify/http-request 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@apify/http-request 可以商用吗?

@apify/http-request 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@apify/http-request 的下载量有多少?

根据 npm 官方下载统计,@apify/http-request 在 2026/10/06 – 2026/11/04 这 30 天内被下载 6,109 次,日均约 204 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@apify/http-request 还在维护吗?

最新版本 2.1.2 发布于 2021/03/02(5 年前);至今共发布 22 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/07/19。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。