gpt4 book ai didi

json - 如何在Laravel 5中获取原始json请求有效载荷?

转载 作者:行者123 更新时间:2023-12-04 13:26:06 24 4
gpt4 key购买 nike

假设这是请求的有效负载:

{name : 'John', age: 42}

您可以使用 Request::all()获取所有参数。

如果有效载荷是像 true42这样的原始类型,如何正确获取请求有效载荷?
Request::getContent()将它们作为字符串接收,我也希望它们也采用正确的类型。

最佳答案

您可以使用json_decode将原始json有效内容解码为数组:

Route::post('/request', function () {
$payLoad = json_decode(request()->getContent(), true);

dd($payLoad['name']);
});

你应该得到
John

或者可以使用 json_decode从查询参数解码有效负载或发布数据:
Route::get('/primitive', function () {
$payLoad = json_decode(request()->get('payload'));

dd($payLoad);
});

打:
http://app.dev/primitive?payload={%20%22name%22:%20%22John%22,%20%22age%22:%2042,%20%22male%22:%20true%20}

你应该得到

enter image description here

关于json - 如何在Laravel 5中获取原始json请求有效载荷?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41452844/

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