php - 在 get 中添加 application/json 标头返回错误 500?
问题描述
我有一个在 codeigniter 4 中开发的 API 服务器和连接到它的颤振应用程序。我在尝试使用 http.get 时遇到问题。当我在标头中添加 application/json 时,它会返回错误 500。但是如果我将其更改为application/x-www-form-urlencoded
它可以工作。此外,当我删除整个 headers 参数时,它也可以工作。这是默认行为吗?
这里是flutter中的示例客户端代码:
dynamic get(String path, {Map<dynamic, dynamic> params}) async {
print(getPath(path, params));
final response = await _client.get(
getPath(path, params),
headers: {
'Content-Type': 'application/json',
},
);
这是我在 CI4 中的控制器:
public function getTest()
{
$testParam = $this->request->getVar('testParam');
$result = $this->model->where('test =' . $testParam . ' AND (IsDeleted IS NULL OR IsDeleted=0)')
->first();
if ($result != null) {
$response['result'] = $result;
$response['msg'] = "Success";
return $this->respond($response);
} else {
return $this->failNotFound('record not found.');
}
}
编辑:这是我得到的错误:
TypeError Object
(
[message:protected] => Argument 2 passed to dot_array_search() must be of the type array, null given, called in C:\xampp\htdocs\api.test\vendor\codeigniter4\framework\system\HTTP\IncomingRequest.php on line 360
似乎参数'Content-Type': 'application/json'
在标题中时变为空。
解决方案
推荐阅读
- python - 如何访问函数内部的变量
- r - 在具有相似名称的列之间使用 for 循环
- google-cloud-platform - 在 GCP 中无法将区域存储桶更改为多区域存储桶的原因是什么?
- javascript - 将 mp3 添加到 React
- python - 无法使用函数更新 tkinter 中的图像
- python - IndexError:数组的索引过多。Numpy + Pandas 数据框
- angular - 如何为新的子路由重新加载 Angular 路由器
- java - 应用程序不断崩溃
- visual-studio-code - 在哪里可以下载旧版本的 Visual Studio Code?
- python - 如何从一个句子中提取所有短语?