首页 > 解决方案 > laravel 5.8 - 邮递员测试 api 请求变为空

问题描述

我遵循本教程并使用postman来测试api. 一切都按预期工作localhost。但是当我上传到生产服务器时,api请求数据被过滤掉了。

我按照说明进行安装passport,以及各种文件中的所有设置。然后我php artisan migrate在生产服务器,在那php artisan passport:install之后,消息说成功:

Encryption keys generated successfully.
Personal access client created successfully.
Client ID: 1
Client secret: <some long strings>
Password grant client created successfully.
Client ID: 2
Client secret: <some long strings>

我有一个测试功能:

ResidentsController.php:

public function testing(Request $request){
        return $request->all();
    // return ['test'=>'success'];
    }

和路线:

api.php:
Route::post('/resident/test','ResidentsController@testing');

所以我postman用来打电话:

POST : http://examplehost/api/resident/test/

有数据:

{"name":"user001"}

和标题:

Content-Type: application/json //<-- which enough for localhost

但数据返回:

[] ///<--- I am expecting {"name":"user001"}

我用标题测试:

[
Content-Type: application/json,
Authorization: {{'bearer'.$accessToken}} ///<-- which the direction from tutorial
]

还是一样:

[]

我不确定我错过了哪一部分。我该如何解决这个问题?

标签: phplaravel

解决方案


在 Postman 中,单击 body,选择 raw 并从下拉列表中选择 JSON(application/json)

在正文中,插入您的 JSON

 {"name":"user001"} 

在您的测试方法中

if ( $request->isJson() ) {
    $json = json_decode(trim($request->getContent()),true);
    return response()->json($json);
}

推荐阅读