# @digitalcredentials/http-client

> An opinionated, isomorphic HTTP client.

Latest version **5.0.4** (published 2024-09-01) · BSD-3-Clause license · 0 weekly downloads

## Install

```sh
npm install @digitalcredentials/http-client
pnpm add @digitalcredentials/http-client
yarn add @digitalcredentials/http-client
bun add @digitalcredentials/http-client
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 5.0.4 |
| Published | 2024-09-01 |
| First published | 2021-05-19 |
| Weekly downloads | 0 |
| License | BSD-3-Clause |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Node | >=18.0 |
| Dependencies | 2 |
| Unpacked size | 22.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Digital Bazaar, Inc. |
| Maintainers | kezike, klemoie, codenamedmitri, kimhd, uligall, alexander.muehle, stuartf, jchartrand, kiliankae |
| Keywords | http, isomorphic, http client |

## Links

- npm: https://www.npmjs.com/package/@digitalcredentials/http-client
- Repository: https://github.com/digitalcredentials/http-client
- Issues: https://github.com/digitalcredentials/http-client/issues
- npm.io page: https://npm.io/package/@digitalcredentials/http-client

## Dependencies (2)

- [ky](https://npm.io/package/ky.md) ^1.0.1
- [undici](https://npm.io/package/undici.md) ^6.6.2

## Recent versions

- 5.0.4 (latest) — 2024-09-01
- 5.0.3 — 2024-09-01
- 5.0.2 — 2024-08-05
- 5.0.1 — 2024-08-04
- 5.0.0 — 2024-08-04
- 1.2.2 — 2021-09-30
- 1.2.1 — 2021-09-30
- 1.0.0 — 2021-05-19

## README

# http-client
[![NPM Version](https://img.shields.io/npm/v/@digitalcredentials/http-client.svg)](https://npm.im/@digitalcredentials/http-client)

> An opinionated, isomorphic HTTP client for Node.js, browsers, and React Native.

### Usage

#### Import httpClient (Node.js)
```js
import https from 'https';
import {httpClient} from '@digitalcredentials/http-client';
```

#### Import httpClient (browsers or React Native)
```js
import {httpClient} from '@digitalcredentials/http-client';
```

#### Import and initialize a custom Bearer Token client
```js
import {httpClient} from '@digitalcredentials/http-client';

const httpsAgent = new https.Agent({rejectUnauthorized: false});

const accessToken = '12345';
const headers = {Authorization: `Bearer ${accessToken}`};

const client = httpClient.extend({headers, httpsAgent});

// subsequent http calls will include an 'Authorization: Bearer 12345' header,
// and use the provided httpsAgent
```

#### GET a JSON response in the browser
```js
try {
  const response = await httpClient.get('http://httpbin.org/json');
  return response.data;
} catch(e) {
  // status is HTTP status code
  // data is JSON error from the server
  const {data, status} = e;
  throw e;
}
```

#### GET a JSON response in Node with an HTTP Agent
```js
import https from 'https';
// use an agent to avoid self-signed certificate errors
const agent = new https.Agent({rejectUnauthorized: false});
try {
  const response = await httpClient.get('http://httpbin.org/json', {agent});
  return response.data;
} catch(e) {
  // status is HTTP status code
  // data is JSON error from the server if available
  const {data, status} = e;
  throw e;
}
```

#### GET HTML by overriding default headers
```js
const headers = {Accept: 'text/html'};
try {
  const response = await httpClient.get('http://httpbin.org/html', {headers});
  // see: https://developer.mozilla.org/en-US/docs/Web/API/Response#methods
  return response.text();
} catch(e) {
  // status is HTTP status code
  // any message from the server can be parsed from the response if present
  const {response, status} = e;
  throw e;
}
```

#### POST a JSON payload
```js
try {
  const response = await httpClient.post('http://httpbin.org/json', {
    // `json` is the payload or body of the POST request
    json: {some: 'data'}
  });
  return response.data;
} catch(e) {
  // status is HTTP status code
  // data is JSON error from the server
  const {data, status} = e;
  throw e;
}
```

#### POST a JSON payload in Node with an HTTP Agent
```js
import https from 'https';
// use an agent to avoid self-signed certificate errors
const agent = new https.Agent({rejectUnauthorized: false});
try {
  const response = await httpClient.post('http://httpbin.org/json', {
    agent,
    // `json` is the payload or body of the POST request
    json: {some: 'data'}
  });
  return response.data;
} catch(e) {
  // status is HTTP status code
  // data is JSON error from the server
  const {data, status} = e;
  throw e;
}
```

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