@zenweb/body
TypeScript icon, indicating that this package has built-in type declarations

4.0.8 • Public • Published

body - 请求内容解析

请求内容解析

功能说明

解析客户端发送的 JSON、Form-urlencoded、Text、二进制 等内容格式。

所支持的解析格式可扩展可自定义。默认支持格式:JSON、Form-urlencoded、Text

演示

import { Context, mapping, Body, $body } from 'zenweb';

export class Controller {
  @mapping({ path: '/', method: 'POST' })
  post(body: Body) {
    console.log(body.type); // POST body 内容类型
    console.log(body.data); // POST Body 内容解析完成后的数据
  }

  @mapping({ path: '/', method: 'POST' })
  async post() {
    console.log(await getAge()); // 类型转换&校验
  }
}

async function getAge() {
  return (await $body.get({ age: '!int' })).age;
}

依赖模块

  • @zenweb/inject
  • @zenweb/helper

配置项

配置项 类型 默认值 功能
encoding string 'utf-8' 客户端未指定情况下,文本内容字符集的默认编码
limit number 1024 * 1024 提交内容尺寸限制,默认:1MB
inflate boolean true 是否支持 http 压缩传输的内容
textTypes string[] ['text/*', 'json', '+json', 'xml', '+xml', 'urlencoded'] 可以被解析成 Text 格式的 mimetype
parses BodyParserClass[] [JSONParser, URLEncodedParser] 内容解析器

Core 挂载项

Context 挂载项

可注入对象

  • singleton

    • BodyOption
  • request

    • RawBody
    • TextBody
    • Body
    • ObjectBody
    • BodyHelper

全局模式

方法 功能
getRawBody() 取得当前请求原始请求内容
getTextBody() 取得当前请求文本内容,经过文字编码转换
getObjectBody() 取得当前请求数据对象
$body BodyHelper 对象快捷方式

其他扩展格式解析

xml

Package Sidebar

Install

npm i @zenweb/body

Weekly Downloads

30

Version

4.0.8

License

MIT

Unpacked Size

27.3 kB

Total Files

14

Last publish

Collaborators

  • yefei777