Skip to content

网络请求 (HTTP)

TIP

提供此服务的插件:@cordisjs/plugin-http。

实例方法

ctx.http(url, init)

ctx.http.get(url, init?)

ctx.http.delete(url, init?)

ctx.http.head(url, init?)

  • url: string | URL 请求地址
  • init: RequestInit 请求配置
  • 返回值: Promise<any>

发送 GET / DELETE / HEAD 请求。

对于 GET / DELETE 请求,返回值的类型取决于 init.decoder

HEAD 请求固定返回 Headers 对象。

ctx.http.post(url, init?)

ctx.http.put(url, init?)

ctx.http.patch(url, init?)

  • url: string | URL 请求地址
  • init: RequestInit 请求配置
  • 返回值: Promise<any>

发送 POST / PUT / PATCH 请求。返回值的类型取决于 init.decoder

ctx.http.ws(url, init?)

  • url: string | URL 请求地址
  • init: RequestInit 请求配置
  • 返回值: WebSocket

创建 WebSocket 连接。

ctx.http.decoder(type, decoder)

  • type: string 编码类型
  • decoder: (raw: Response) => any 解码器
  • 返回值: () => void 回收副作用

添加解码器。

ctx.http.proxy(protocols, factory)

  • protocols: string[] 协议列表
  • factory: (url: URL) => Dispatcher 代理工厂
  • 返回值: () => void 回收副作用

添加代理协议。

TIP

使用示例可以参考 @cordisjs/plugin-http-socks 插件。

实例事件

'http/fetch'(url, init, config, next)

  • 触发模式: waterfall
  • url: URL 请求地址
  • init: RequestInit 请求配置
  • config: RequestInit 请求配置
  • next: () => Promise<Response> 下一个中间件
  • 返回值: Promise<Response>