gpt4 book ai didi

php - 如何在 Laravel 5.0 中获取 $request->content?

转载 作者:行者123 更新时间:2023-12-02 05:33:03 28 4
gpt4 key购买 nike

我是 Laravel 的新手,在获取我发布到我正在编写的 REST api 的 JSON 时遇到了一些问题。

更新为清楚起见,这是:

    $content = json_decode($request->content);
var_dump($content);
exit;

同时返回null

原创

这是我的store 方法:

public function store(Request $request)
{
// Creates a new user based on the passed JSON
// I appreciate this wont work as it's json encoded, but this was my
// last test.
// Previously I'd tried: $content = json_decode($request->content);
// but that was also null :(
$user = new User();
$user->name = $request->content["name"];
$user->email = $request->content['email'];
$user->password = $request->content['password'];

var_dump($request); exit;
// Commit to the database
$user->save();
}

这是我要发送的内容(通过:我只是休息客户端):

{
"name":"Steve Jobs 2",
"email":"s@trp2.com",
"password":"something123",
}

下面是将 var_dump 作为响应呈现时的结果:

      protected 'cacheControl' => 
array (size=0)
empty
protected 'content' => string '{
"name":"Steve Jobs 2",
"email":"s@trp2.com",
"password":"something123",
}' (length=85)
protected 'languages' => null
protected 'charsets' => null
protected 'encodings' => null

因此我可以在 Request 对象中看到 content,但无论我尝试什么,它始终为 null。所以我的问题是,我究竟该如何访问它?!

谢谢!

最佳答案

您可能想使用 $request->getContent()

关于php - 如何在 Laravel 5.0 中获取 $request->content?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29364410/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com