首页 > 解决方案 > 在 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'在标题中时变为空。

标签: phpfluttercodeigniter-4

解决方案


推荐阅读