Laravel API不接受来自Postman的JSON请求

rqqzpn5f  于 2022-11-07  发布在  Postman
关注(0)|答案(3)|浏览(351)

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请求:

表单数据请求:

为什么它不工作的情况下JSONcontent-typeapplication/jsonAccept:*/*??

gblwokeq

gblwokeq1#

JSON中不允许使用注解。字段Body -〉raw -〉json中存在错误

cdmah0mi

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

hwamh0ep

hwamh0ep3#

在我的情况下,它是缺乏Content-Length头。
值应为请求正文的字符数。
Content-Type: application/json也应该存在。

相关问题