php - 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
]
还是一样:
[]
我不确定我错过了哪一部分。我该如何解决这个问题?
解决方案
在 Postman 中,单击 body,选择 raw 并从下拉列表中选择 JSON(application/json)
在正文中,插入您的 JSON
{"name":"user001"}
在您的测试方法中
if ( $request->isJson() ) {
$json = json_decode(trim($request->getContent()),true);
return response()->json($json);
}
推荐阅读
- reactjs - 如何使用 ReactQuery 中的 useQuery 挂钩来更新状态?
- r - 转换字符列,然后使用 r mutate 将其拆分为多个新的布尔列
- c++ - 如何模拟 find_package() 与 add_subdirectory() 一样?(现代 CMake)
- php - 在用户数据脚本中的 EC2 AWS 实例中运行 composer 命令不工作
- python - 最小硬币找零问题 - 回溯
- javascript - 如何让我的小文本低于我的大文本?
- javascript - useEffect 和函数体有什么区别?
- git - 是否可以从 git rev-list 获得深度优先对象排序?
- c# - 如何使用 for 循环简化此代码以更新许多控件?
- javascript - HTML5 音频标签音量控制不起作用