Slim 4. POST 请求正文为空
Slim 4. POST Request body is null
我使用方法 POST 创建了路线。当我发送带有正文的请求时,但我尝试使用
$request->getParsedBody()
我总是取NULL。为什么?
<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
require __DIR__ . '/../vendor/autoload.php';
$app = AppFactory::create();
$app->addErrorMiddleware(true, false, false);
$app->post('/add', function (Request $request, Response $response, array $args) {
$data = $request->getParsedBody();
return $response;
});
$app->run();
邮递员的请求
POST /test HTTP/1.1
Host: localhost:8080
Content-Type: application/json
Cache-Control: no-cache
{
"name": "test",
"email": "test@mail.ru"
}
尝试添加正文解析中间件
$app->addBodyParsingMiddleware();
我使用方法 POST 创建了路线。当我发送带有正文的请求时,但我尝试使用
$request->getParsedBody()
我总是取NULL。为什么?
<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
require __DIR__ . '/../vendor/autoload.php';
$app = AppFactory::create();
$app->addErrorMiddleware(true, false, false);
$app->post('/add', function (Request $request, Response $response, array $args) {
$data = $request->getParsedBody();
return $response;
});
$app->run();
邮递员的请求
POST /test HTTP/1.1
Host: localhost:8080
Content-Type: application/json
Cache-Control: no-cache
{
"name": "test",
"email": "test@mail.ru"
}
尝试添加正文解析中间件
$app->addBodyParsingMiddleware();