Laravel 如何在使用 api 调用时解码 HTTP 请求 body Content-Type: application/x-www-form-urlencoded
How is Laravel decoding HTTP request body Content-Type: application/x-www-form-urlencoded when using api call
我正在使用 Laravel-5.5,并且正在为 iOS 应用程序开发 apis。
在这里,我使用邮递员呼叫所有 api。我有一个注册 api 和两个 headers:
1) Authorization: Bearer access_token
2) Content-type: application/x-www-form-urlencoded
现在当我请求这个 api 我只想打印 dd($request->all());
但它没有给我正确的请求数据。我试过 json_decode(urldecode(file_get_contents('php://input')));
解码它,也试过 $request->getContent();
但没有成功!
有谁知道使用Content-type: application/x-www-form-urlencoded
时如何在控制器中获取请求数据吗??
编辑:
public function registration(Request $request)
{
dd($request->all());
dd($request->getContent()); //also tried but not work
dd(json_decode(urldecode(file_get_contents('php://input')))); //no luck
}
我是这样修复的:
$requestData = json_decode($request->getContent(), true);
dd($requestData);
在 postman 中传递 headers Content-Type: application/json
并修复!!
您只需要这样做:
$requestData = json_decode($request->getContent(), true);
并且你可以在你的请求中获取到你控制器中的所有请求数据!
希望这对以后的用户有所帮助!!
我正在使用 Laravel-5.5,并且正在为 iOS 应用程序开发 apis。
在这里,我使用邮递员呼叫所有 api。我有一个注册 api 和两个 headers:
1) Authorization: Bearer access_token
2) Content-type: application/x-www-form-urlencoded
现在当我请求这个 api 我只想打印 dd($request->all());
但它没有给我正确的请求数据。我试过 json_decode(urldecode(file_get_contents('php://input')));
解码它,也试过 $request->getContent();
但没有成功!
有谁知道使用Content-type: application/x-www-form-urlencoded
时如何在控制器中获取请求数据吗??
编辑:
public function registration(Request $request)
{
dd($request->all());
dd($request->getContent()); //also tried but not work
dd(json_decode(urldecode(file_get_contents('php://input')))); //no luck
}
我是这样修复的:
$requestData = json_decode($request->getContent(), true);
dd($requestData);
在 postman 中传递 headers Content-Type: application/json
并修复!!
您只需要这样做:
$requestData = json_decode($request->getContent(), true);
并且你可以在你的请求中获取到你控制器中的所有请求数据!
希望这对以后的用户有所帮助!!