Slim 3 getParsedBody() always null and empty(超薄3 getParsedBody()始终为空)
问题描述
我正在使用超薄框架版本3,遇到一些问题。
$app-> post('/', function($request, $response){
$parsedBody = $request->getParsedBody()['email'];
var_dump($parsedBody);
});
结果始终为:
空
你能帮我吗?
推荐答案
这取决于您将数据发送到路由的方式。这是POST路由,因此默认情况下,它将正文数据设置为标准格式(application/x-www-form-urlencoded
)。
Content-type
头设置为application/json
。例如,卷曲将如下所示:
curl -X POST -H "Content-Type: application/json"
-d '{"email": "a@example.com"}' http://localhost/
此外,您还应该验证您要查找的数组密钥是否存在:
$parsedBody = $request->getParsedBody()
$email = $parsedBody['email'] ?? false;
这篇关于超薄3 getParsedBody()始终为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!