middlewares/payload

中间件,用于解析请求体,支持json、csv和url-encode

v3.0.2 2024-01-12 17:57 UTC

This package is auto-updated.

Last update: 2024-09-12 19:19:35 UTC


README

Latest Version on Packagist Software License Testing Total Downloads

如果请求体未解析且方法为POST、PUT或DELETE,则解析请求体。它包含以下组件以支持不同格式

解析请求体失败将抛出 Middlewares\Utils\HttpErrorException。有关更多信息,请参阅 middlewares/utils

要求

安装

此包可通过Composer以 middlewares/payload 的方式安装和自动加载。

composer require middlewares/payload

JsonPayload

解析请求的JSON有效载荷。

Dispatcher::run([
    (new Middlewares\JsonPayload())
        ->associative(false)
        ->depth(64)
]);

$response = $dispatcher->dispatch(new ServerRequest());

包含以下选项以配置 json_decode 函数

associative

默认启用,将对象转换为关联数组。

//Disable associative arrays
$payload = (new Middlewares\JsonPayload())->associative(false);

depth

配置 json_decode 的递归深度选项。默认为 512

options

传递 json_decode 选项的掩码:JSON_BIGINT_AS_STRING(默认启用),JSON_OBJECT_AS_ARRAYJSON_THROW_ON_ERROR

methods

配置允许的方法。默认情况下,只有方法为 POST, PUT, PATCH, DELETE, COPY, LOCK, UNLOCK 的请求被处理。

//Parse json only with POST and PUT requests
$payload = (new Middlewares\JsonPayload())->methods(['POST', 'PUT']);

contentType

配置请求中允许的所有 Content-Type 标头。默认为 application/json

//Parse json only in request with these two Content-Type values
$payload = (new Middlewares\JsonPayload())->contentType(['application/json', 'text/json']);

override

如果存在,则覆盖之前解析的请求体(默认为 false

UrlEncodePayload

解析请求的url-encoded有效载荷。

Dispatcher::run([
    new Middlewares\UrlEncodePayload()
]);

methods

配置允许的方法。默认情况下,只有方法为 POST, PUT, PATCH, DELETE, COPY, LOCK, UNLOCK 的请求被处理。

contentType

配置请求中允许的所有 Content-Type 标头。默认为 application/x-www-form-urlencoded

override

如果存在,则覆盖之前解析的请求体(默认为 false

CsvPayload

CSV有效载荷由 middlewares/csv-payload 包支持。

XmlPayload

解析请求的XML有效载荷。解析后的请求体将返回一个 SimpleXMLElement 实例。

methods

配置允许的方法。默认情况下,只有方法为 POST, PUT, PATCH, DELETE, COPY, LOCK, UNLOCK 的请求被处理。

contentType

配置请求中允许的所有 Content-Type 标头。默认为 text/xmlapplication/xmlapplication/x-xml

override

如果存在,则覆盖之前解析的请求体(默认为 false

有关最近更改的更多信息,请参阅 CHANGELOG,有关贡献的详细信息,请参阅 CONTRIBUTING

MIT许可证(MIT)。有关更多信息,请参阅 LICENSE