超薄3 getParsedBody()始终为空 [英] Slim 3 getParsedBody() always null and empty

查看:21
本文介绍了超薄3 getParsedBody()始终为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用超薄框架版本3,遇到一些问题。

$app-> post('/', function($request, $response){
  $parsedBody = $request->getParsedBody()['email'];
  var_dump($parsedBody);
});

结果始终为:

你能帮我吗?

推荐答案

这取决于您将数据发送到路由的方式。这是POST路由,因此默认情况下,它将正文数据设置为标准格式(application/x-www-form-urlencoded)。

如果要将JSON发送到此路由,则需要将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()始终为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆