# @applitools/http-commons

Latest version **3.0.2** (published 2024-11-13) · Apache-2.0 license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @applitools/http-commons
pnpm add @applitools/http-commons
yarn add @applitools/http-commons
bun add @applitools/http-commons
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 3.0.2 |
| Published | 2024-11-13 |
| First published | 2017-10-12 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=18.0.0 |
| Dependencies | 6 |
| Unpacked size | 79.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Gil Tayar |
| Maintainers | hedva.slassi, netta.bondy, arik-applitools, shai.shlomai, roeefr, livgerti, shiran.sidis, mmilapp, anandbagmar, dockermaster, mkowalew3382, itaiz134, anastasia.koifman, eirenik0, sergovapplitools, fatihsolhan-applitools, grayscale64, itaibh-applitools, bongo123, aretm_borodavka, netaavivi, applitoolsmustafa, zamboney, benbaba, amit.rokach, eladheller, adamcarmi, amir.groisman, noamshv, noam.gaash, giladgd, ran-itzhaki, noam.mendel, gearm, benny.halberstadt, gofilord, alex.burdeynyy, david.haeffner, chaimaharonson, roy.sela, ronikar_applitools, danielputerman, amitzur, ramapplitools, applitools-admin, applitools-readonly, iasisapp, yotammadem, liranbarokas, itayy.applitools, denis.styrt, kyrylo.onufriiev, amitzur-applitools, yardenw.applitools, ormeda, vgprod, clementbarry |
| Keywords | applitools, testing, browser, grid, visual-testing, nodejs, javascript |

## Links

- npm: https://www.npmjs.com/package/@applitools/http-commons
- Repository: https://github.com/applitools/rendering-grid
- Homepage: https://github.com/applitools/rendering-grid#readme
- Issues: https://github.com/applitools/rendering-grid/issues
- npm.io page: https://npm.io/package/@applitools/http-commons

## Dependencies (6)

- [debug](https://npm.io/package/debug.md) ^4.1.1
- [node-fetch](https://npm.io/package/node-fetch.md) ^2.6.0
- [lodash.merge](https://npm.io/package/lodash.merge.md) ^4.6.2
- [agentkeepalive](https://npm.io/package/agentkeepalive.md) ^4.1.0
- [@applitools/functional-commons](https://npm.io/package/@applitools/functional-commons.md) ^2.0.2
- [@applitools/monitoring-commons](https://npm.io/package/@applitools/monitoring-commons.md) ^2.0.3

## Alternatives

- [@lexical/table](https://npm.io/package/@lexical/table.md) — 3.0M weekly downloads
- [mantine-datatable](https://npm.io/package/mantine-datatable.md) — 98.2K weekly downloads
- [react-native-collapsible-tab-view](https://npm.io/package/react-native-collapsible-tab-view.md) — 70.6K weekly downloads
- [@handsontable/vue3](https://npm.io/package/@handsontable/vue3.md) — 16.1K weekly downloads
- [vuewordcloud](https://npm.io/package/vuewordcloud.md) — 7.2K weekly downloads

## Recent versions

- 3.0.2 (latest) — 2024-11-13
- 3.0.1-fixHttpAgent.1 — 2024-11-06
- 3.0.0 — 2023-09-05
- 2.4.7 — 2022-05-30
- 2.4.5 — 2021-09-29
- 2.4.3 — 2020-02-26
- 2.4.1 — 2020-02-26
- 2.3.12 — 2019-09-19
- 2.3.10 — 2019-09-01
- 1.3.9 — 2019-08-14
- 1.3.7 — 2019-07-22
- 1.3.5 — 2019-06-19
- 1.3.3 — 2019-05-01
- 1.3.1 — 2019-05-01
- 1.2.9 — 2019-03-24
- … 28 more at https://npm.io/package/@applitools/http-commons/versions

## README

<!-- markdownlint-disable MD024 -->

# http-commons

Library that has http common functionality, all around http-client currently.

## Installing

```sh
npm install @applitools/http-commons
```

## Using the package

Let's see an example with `fetchAsJson`

```js
const {fetchAsJson} = require('@applitools/http-commons')

await fetchAsJson('https://swapi.co/api/people/1/') // ===> {name: "Luke Skywalker", ...}
```

## API

All these functions with throw an exception if the status code is not 2xx.
The excption will have the following properties:

* `code`: it will be `'ERR_X_STATUS_CODE_NOT_OK'`
* `status`: the HTTP status code
* `statusText`: the HTTP status text
* `headers`: an object with the response headers

### `fetchAsBuffer(url, [fetchOptions], [options])`

async fetches URL and returns a Buffer response.

#### `url`

The URL to fetch.

#### `fetchOptions`

The fetch options [used by the `node-fetch` package](https://www.npmjs.com/package/node-fetch#fetch-options).

#### `options`

The following options are available:

##### `alternativeFetch`

(For testing purposes) A function that will be used as an alternative to `node-fetch`-s `fetch` function.

#### returns

An object with the response body as JSON parsed.

#### Example

```js
await fetchAsJson('https://swapi.co/api/people/1/') // ===> {name: "Luke Skywalker", ...}
```

### `fetchAsText(url, [fetchOptions], [options])`

async fetches URL and returns the response as a string.

#### `url`

The URL to fetch.

#### `fetchOptions`

The fetch options [used by the `node-fetch` package](https://www.npmjs.com/package/node-fetch#fetch-options).

#### `options`

The following options are available:

##### `alternativeFetch`

(For testing purposes) A function that will be used as an alternative to `node-fetch`-s `fetch` function.

#### returns

A string with the response body.

#### Example

```js
await fetchAsText('https://www.wikipedia.org')) // ===> "<!DOCTYPE html><html ..."
```

### `fetchAsTextWithJsonBody(url, json, [fetchOptions], [options])`

async posts URL with a JSON body and returns the response as a string.

#### `url`

The URL to fetch.

#### `fetchOptions`

The fetch options [used by the `node-fetch` package](https://www.npmjs.com/package/node-fetch#fetch-options).
Note that the default options are `{method: 'POST', body: '_the_json_'}`, with the correct `content-type` header,
but you can override this using `fetchOptions`

#### `options`

The following options are available:

##### `alternativeFetch`

(For testing purposes) A function that will be used as an alternative to `node-fetch`-s `fetch` function.

#### returns

A string with the response body.

#### Example

```js
await fetchAsTextWithJsonBody('https://httpbin.org/anything', {x: 4})) // ===> "{..{"x": 4}..}"
```

### `fetchAsJsonWithJsonBody(url, json, [fetchOptions], [options])`

async posts URL with a JSON body and returns the response as a string.

#### `url`

The URL to fetch.

#### `fetchOptions`

The fetch options [used by the `node-fetch` package](https://www.npmjs.com/package/node-fetch#fetch-options).
Note that the default options are `{method: 'POST', body: '_the_json_'}`, with the correct `content-type` header,
but you can override this using `fetchOptions`

#### `options`

The following options are available:

##### `alternativeFetch`

(For testing purposes) A function that will be used as an alternative to `node-fetch`-s `fetch` function.

#### returns

A "JSON" object with the parsed body

#### Example

```js
await fetchAsTextWithJsonBody('https://httpbin.org/anything', {x: 4}, {method: 'PUT'})) // ===> {..{"x": 4}..}
```

### `fetchAsBufferWithJsonBody(url, json, [fetchOptions], [options])`

async posts URL with a JSON body and returns the response as a string.

#### `url`

The URL to fetch.

#### `fetchOptions`

The fetch options [used by the `node-fetch` package](https://www.npmjs.com/package/node-fetch#fetch-options).
Note that the default options are `{method: 'POST', body: '_the_json_'}`, with the correct `content-type` header,
but you can override this using `fetchOptions`

#### `options`

The following options are available:

##### `alternativeFetch`

(For testing purposes) A function that will be used as an alternative to `node-fetch`-s `fetch` function.

#### returns

A buffer with the response body.

#### Example

```js
await fetchAsBufferWithJsonBody('https://httpbin.org/anything', {x: 4})) // ===> Buffer (....)
```

### `retryFetch(func, options)`

Retries code and deals correctly with retrying HTTP and connection errors.

#### `func`

An async func that calls one of the `fetch*` functions above or the `fetch` in `node-fetch` directly.

#### `options`

##### `retries`

Number of retries before failing.

##### `sleepTime`

The time (in ms) for sleeping between retries

##### `backoff`

Exponential backoff factor for `sleepTime`

##### `idempotent`

Some errors, like `5xx` http status error should not retry if the `fetch` operation is idempotent. So this
flag says whether the operation is idempotent to know whether to retry.

#### returns

Whatever `func` returns

#### Example

```js
const json = await retry(() => fetchAsJson('http://httpbin.org/anything'), {idempotent: true})
```

---
_Source: https://npm.io/package/@applitools/http-commons · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
