# httpx

> http(s) module with power

Latest version **3.0.1** (published 2024-04-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install httpx
pnpm add httpx
yarn add httpx
bun add httpx
```

## Health

**Score 35/100 (D)** — status: abandoned.

Positive: esm support; no vulnerabilities; high maintenance score.

Warnings: low downloads; no types.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 3.0.1 |
| Published | 2024-04-01 |
| First published | 2015-02-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Node | >= 12 |
| Dependencies | 1 |
| Unpacked size | 13.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 50 |
| Author | Jackson Tian |
| Maintainers | jacksontian |

## Links

- npm: https://www.npmjs.com/package/httpx
- Repository: https://github.com/JacksonTian/httpx
- Issues: https://github.com/JacksonTian/httpx/issues
- npm.io page: https://npm.io/package/httpx

## Dependencies (1)

- [debug](https://npm.io/package/debug.md) ^4.1.1

## Recent versions

- 3.0.1 (latest) — 2024-04-01
- 2.3.3 (v2.x-latest) — 2024-11-07
- 3.0.0 — 2024-04-01
- 2.3.2 — 2024-02-22
- 2.3.1 — 2023-11-23
- 2.3.0 — 2023-09-20
- 2.2.7 — 2020-11-09
- 2.2.6 — 2020-10-16
- 2.2.5 — 2020-10-15
- 2.2.4 — 2020-06-11
- 2.2.3 — 2020-02-12
- 2.2.2 — 2019-11-01
- 2.2.1 — 2019-10-30
- 2.2.0 — 2019-05-28
- 2.1.4 — 2018-09-10
- … 6 more at https://npm.io/package/httpx/versions

## README

# httpx

http(s) module with power.

[![NPM version][npm-image]][npm-url]
[![Node.js CI](https://github.com/JacksonTian/httpx/actions/workflows/node.js.yml/badge.svg)](https://github.com/JacksonTian/httpx/actions/workflows/node.js.yml)
[![codecov][cov-image]][cov-url]
[![npm download][download-image]][download-url]

[npm-image]: https://img.shields.io/npm/v/httpx.svg?style=flat-square
[npm-url]: https://npmjs.org/package/httpx
[cov-image]: https://codecov.io/gh/JacksonTian/httpx/branch/master/graph/badge.svg
[cov-url]: https://codecov.io/gh/JacksonTian/httpx
[download-image]: https://img.shields.io/npm/dm/httpx.svg?style=flat-square
[download-url]: https://npmjs.org/package/httpx

## Installation

```bash
npm install httpx --save
```

## Usage

```js
import * as httpx from 'httpx';
```

### Request URL

```js
const response = await httpx.request('http://www.baidu.com/');
const body = await httpx.read(response, 'utf-8');
console.log(body);
```

### Request SSE URL

```js
const response = await httpx.request('sse url');
for await (const event of httpx.readAsSSE(response)) {
  console.log(event);
}
```

## API

### `httpx.request(url[, options])`

It returns `Promise<Response>`.

Requests the url with options, then return the response.

- **url** String | Object - The URL to request, either a String or a Object that return by [url.parse](http://nodejs.org/api/url.html#url_url_parse_urlstr_parsequerystring_slashesdenotehost).
- ***options*** Object - Optional
  - ***method*** String - Request method, defaults to `GET`. Could be `GET`, `POST`, `DELETE` or `PUT`.
  - ***data*** String | [Buffer](http://nodejs.org/api/buffer.html) | Readable - Manually set the content of payload.
  - ***headers*** Object - Request headers.
  - ***timeout*** Number - Request timeout in milliseconds. Defaults to 3000. When timeout happen, will return `RequestTimeout`.
  - ***agent*** [http.Agent](http://nodejs.org/api/http.html#http_class_http_agent) - HTTP/HTTPS Agent object.
      Set `false` if you does not use agent.
  - ***beforeRequest*** Function - Before request hook, you can change every thing here.
  - ***compression*** Boolean - Enable compression support. Tell server side responses compressed data

### `httpx.read(response[, encoding])`

It returns `Promise<Buffer | String>`.

Consume the response and read all data from the response.

- **response** Response - the Client response. Don't setEncoding() for the response.
- **encoding** String - Optional. If specify the encoding, will return String. If not specify encoding, return the buffer.

### `httpx.readAsSSE(response)`

It returns `AsyncGenerator<Event, void, unknown>`.

Consume the response data with async iterator.

- **response** Response - the Client response. Don't setEncoding() for the response.

## Using with http proxy

```js
import { SocksProxyAgent } from 'socks-proxy-agent';
import * as httpx from 'httpx';

await httpx.request('http://www.baidu.com/', {
  // pass a http proxy agent
  agent: new SocksProxyAgent('socks://your_proxy_server:3001')
});
```

## License

The MIT license

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