Laravel API不接受JSON
请求。如果我以表单数据的形式请求,它可以工作,但如果我在 Postman 正文中发布JSON
对象,它就不接收请求数据。
给药途径:
$router->group(['prefix' => 'imp'], function () use ($router) {
$router->group(['prefix' => 'lead'], function () use ($router) {
$router->post('/jardy', 'FeedController@jardy');
});
});
控制器:
public function jardy(Request $request)
{
$this->validate($request, [
'api_key' => 'required',
]);
$api_key = $request->input('api_key');
return $api_key;
}
JSON
请求:
表单数据请求:
为什么它不工作的情况下JSON
,content-type
是application/json
,Accept:*/*
??
3条答案
按热度按时间gblwokeq1#
JSON中不允许使用注解。字段Body -〉raw -〉json中存在错误
cdmah0mi2#
您必须添加标题
接受:应用程序/json
然后,laravel将您的RAW json解析为输入变量,可以使用-〉input()访问它们
请参阅:x1c 0d1x
使用 / 这是 Postman 的默认设置,将不起作用。如果你不想中继头,你也可以做
$request->json()
,但我猜,你只是想传递头。请参阅处理此问题的源代码:https://github.com/laravel/framework/blob/7.x/src/Illuminate/Http/Concerns/InteractsWithContentTypes.php#L52
和https://github.com/laravel/framework/blob/7.x/src/Illuminate/Http/Concerns/InteractsWithContentTypes.php#L32
hwamh0ep3#
在我的情况下,它是缺乏
Content-Length
头。值应为请求正文的字符数。
Content-Type: application/json
也应该存在。