# @har-tools/http-message

> JavaScript API to create an HTTP message from Fetch API requests/responses.

Latest version **0.1.2** (published 2023-10-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install @har-tools/http-message
pnpm add @har-tools/http-message
yarn add @har-tools/http-message
bun add @har-tools/http-message
```

## Health

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

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.2 |
| Published | 2023-10-12 |
| First published | 2023-10-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 16 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Artem Zakharchenko |
| Maintainers | kettanaito |
| Keywords | http, message, request, response |

## Links

- npm: https://www.npmjs.com/package/@har-tools/http-message
- npm.io page: https://npm.io/package/@har-tools/http-message

## Dependencies (2)

- [outvariant](https://npm.io/package/outvariant.md) ^1.4.0
- [@open-draft/deferred-promise](https://npm.io/package/@open-draft/deferred-promise.md) ^2.2.0

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 0.1.2 (latest) — 2023-10-12
- 0.1.1 — 2023-10-11
- 0.1.0 — 2023-10-11

## README

# `@har-tools/http-message`

JavaScript API to create an HTTP message from Fetch API requests/responses.

## Installation

```sh
npm install @har-tools/http-message
```

## API

### `new HttpMessage(startLine, headers, body)`

Creates a new HTTP message from the given start line, heades, and optional body.

### `HttpMessage.fromRequest(request)`

- `request: Request`

Creates a new HTTP message from the given request instance.

```js
const request = new Request('https://example.com', {
  method: 'POST',
  body: 'Hello world!',
})
const message = HttpMessage.from(request)

console.log(message)
// POST https://example.com HTTP/1.0
// content-type: text/plain;charset=UTF-8
//
// Hello world!
```

### `HttpMessage.fromResponse(response)`

- `response: Response | http.IncomingMessage`

Creates a new HTTP message from the given response instane.

```js
const response = new Response(
  JSON.stringify({
    id: 1,
    name: 'John',
  }),
  {
    status: 201,
    headers: {
      'Content-Type': 'application/json',
      'X-Custom-Header': 'Value',
    },
  }
)
const message = HttpMessage.from(response)

console.log(message)
// HTTP/1.0 201 Created
// content-type: application/json
// x-custom-header: Value
//
// {"id":1,"name":"John"}
```

> Make sure to _clone the response_ before passing it to the `.fromResponse()` method. This method reads the response body.

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