Slim 4. POST 请求正文为空
我使用 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": "[email protected]"
}
I created route with method POST. When i send request with body, but i trying use
$request->getParsedBody()
I am always takes NULL. Why?
<?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();
Request from Postman
POST /test HTTP/1.1
Host: localhost:8080
Content-Type: application/json
Cache-Control: no-cache
{
"name": "test",
"email": "[email protected]"
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试添加正文解析中间件
Try to add the Body Parsing Middleware