@colyseus/httpie

中文文档
2.0.1发布于 2025/05/31MITTypeScriptESM + CJS零依赖近 30 天下载 14.4万

A lightweight, Promise-based HTTP client for Node.js and the browser~!

原始使用文档
文档目录12 节
  1. Features
  2. Install
  3. Usage
  4. API
  5. send(method, url, opts={})
  6. get(url, opts={})
  7. post(url, opts={})
  8. put(url, opts={})
  9. patch(url, opts={})
  10. del(url, opts={})
  11. Error Handling
  12. License
httpie
A Node.js and browser HTTP client as easy as pie!

Features

  • Promise- based HTTP requestor
  • Works with HTTP and HTTPS protocols
  • Automatically handles JSON requests and responses
  • Extremely lightweight with no dependencies 678 bytes!
  • Includes aliases for common HTTP verbs: get, post, put, patch, and del

Additionally, this module is delivered as:

Install

$ npm install --save httpie

Usage

Note: The async syntax is for demo purposes – you may use Promises in a Node 6.x environment too!

import { get, post } from 'httpie';

try {
  const { data } = await get('https://pokeapi.co/api/v2/pokemon/1');

  // Demo: Endpoint will echo what we've sent
  const res = await post('https://jsonplaceholder.typicode.com/posts', {
    body: {
      id: data.id,
      name: data.name,
      number: data.order,
      moves: data.moves.slice(0, 6)
    }
  });

  console.log(res.statusCode); //=> 201
  console.log(res.data); //=> { id: 1, name: 'bulbasaur', number: 1, moves: [{...}, {...}] }
} catch (err) {
  console.error('Error!', err.statusCode, err.message);
  console.error('~> headers:', err.headers);
  console.error('~> data:', err.data);
}

API

send(method, url, opts={})

Returns: Promise

Any httpie.send request (and its aliases) will always return a Promise.

If the response's statusCode is 400 or above, this Promise will reject with a formatted error – see Error Handling. Otherwise, the Promise will resolve with the full ClientRequest stream.

The resolved response will receive a new data key, which will contain the response's full payload. Should the response return JSON content, then httpie will parse it and the res.data value will be the resulting JSON object!

method

Type: String

The HTTP method name – it must be uppercase!

url

Type: String or URL

If url is a string, it is automatically parsed with url.parse() into an object.

opts.body

Type: Mixed
Default: undefined

The request's body, can be of any type!

Any non-Buffer objects will be converted into a JSON string and the appropriate Content-Type header will be attached.

Additionally, httpie will always set a value for the Content-Length header!

opts.headers

Type: Object
Default: {}

The custom headers to send with your request.

opts.redirect

Type: Boolean
Default: true

Whether or not redirect responses should be followed automatically.

Note: This may only happen with a 3xx status and if the response had a Location header.

opts.reviver

Type: Function
Default: undefined

An optional function that's passed directly to JSON.parse, allowing you transform aspects of the response data before the httpie request resolves.

Note: This will only run if httpie detects that JSON is contained in the response!

opts.timeout

Type: Integer
Default: undefined

The time, in milliseconds, before automatically terminating the request.

When the request exceeds this limit, httpie rejects with an err<Error>, adding a truthy err.timeout value.

Important: There is a slight behavioral difference between the Node & browser versions!
In the server, the timeout value does not propagate to any redirects.
In the browser, the timeout value will not reset during redirects.

opts.withCredentials

Type: Boolean
Default: false

Whether or not cross-site requests should include credentials (such as cookies).
This value is passed directly to XMLHttpRequest.withCredentials.

Important: This is for the browser version only!

get(url, opts={})

Alias for send('GET', url, opts).

post(url, opts={})

Alias for send('POST', url, opts).

put(url, opts={})

Alias for send('PUT', url, opts).

patch(url, opts={})

Alias for send('PATCH', url, opts).

del(url, opts={})

Alias for send('DELETE', url, opts).

Error Handling

All responses with statusCode >= 400 will result in a rejected httpie request. When this occurs, an Error instance is formatted with complete information:

  • err.message – String – Identical to err.statusMessage;
  • err.statusMessage – String – The response's statusMessage value;
  • err.statusCode – Number – The response's statusCode value;
  • err.headers – Object – The response's headers object;
  • err.data – Mixed – The response's payload;

Additionally, errors that are a result of a timeout expiration will have a truthy err.timeout value.

Important: The error's data property may also be parsed to a JSON object, according to the response's headers.

import { get } from 'httpie';

get('https://example.com/404').catch(err => {
  console.error(`(${err.statusCode}) ${err.message}`)
  console.error(err.headers['content-type']);
  console.error(`~> ${err.data}`);
});
//=> "(404) Not Found"
//=> "text/html; charset=UTF-8"
//=> ~> <?xml version="1.0" encoding="iso-8859-1"?>\n<!DOCTYPE html ...</body>\n</html>

License

MIT © Luke Edwards

版本与发布记录

@colyseus/httpie 共发布 2 个版本,首次发布于 2025/05/31;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.0.1latest2025/05/311.4 年前
2.0.02025/05/311.4 年前

关于 @colyseus/httpie 的常见问题

@colyseus/httpie 是什么?

A lightweight, Promise-based HTTP client for Node.js and the browser~!

如何安装 @colyseus/httpie?

在项目目录运行 npm install @colyseus/httpie,或使用 pnpm add / yarn add / bun add。

@colyseus/httpie 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@colyseus/httpie 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@colyseus/httpie 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=10。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@colyseus/httpie 可以商用吗?

@colyseus/httpie 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@colyseus/httpie 的下载量有多少?

根据 npm 官方下载统计,@colyseus/httpie 在 2026/10/11 – 2026/11/09 这 30 天内被下载 144,226 次,日均约 4,808 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@colyseus/httpie 还在维护吗?

最新版本 2.0.1 发布于 2025/05/31(1.4 年前);至今共发布 2 个版本,其中过去 12 个月发布了 0 个;首次发布于 2025/05/31。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。