npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@apify/http-request

v2.1.2

Published

Wrapper of got package used across Apify

Downloads

754

Readme

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.