Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

@zenweb/body

Package Overview
Dependencies
Maintainers
1
Versions
50
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@zenweb/body

Zenweb Body module

latest
npmnpm
Version
5.1.1
Version published
Weekly downloads
20
300%
Maintainers
1
Weekly downloads
 
Created
Source

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

配置项

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

Core 挂载项

Context 挂载项

可注入对象

  • singleton

    • BodyOption
  • request

    • RawBody
    • TextBody
    • Body
    • ObjectBody
    • BodyHelper

全局模式

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

其他扩展格式解析

xml

Keywords

web

FAQs

Package last updated on 14 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts