简体   繁体   中英

Cakephp4 don't accept Content Type application/json; charset=utf-8

I upgrade my application from Cakephp3 to cakephp4 but unfortunately I am unable to update the android app accordingly due to logistic issues. The android app makes HTTP calls with Content-Type application/json; charset=utf-8 application/json; charset=utf-8 and cakephp4 keeps throwing bad request. Is there a way to get cakephp4 to accept this header as a stop gap measure before I collect all the tablets and update them accordingly?

Below is the error log:

1) App\Test\TestCase\Controller\Api\EmployeesControllerTest::testJwtTokenPost
Possibly related to Cake\Http\Exception\BadRequestException: "Bad Request"
#0 /var/www/html/vendor/cakephp/cakephp/src/Http/Runner.php(73): Cake\Http\Middleware\BodyParserMiddleware->process(Object(Cake\Http\ServerRequest), Object(Cake\Http\Runner))
#1 /var/www/html/vendor/cakephp/cakephp/src/Routing/Middleware/RoutingMiddleware.php(166): Cake\Http\Runner->handle(Object(Cake\Http\ServerRequest))
#2 /var/www/html/vendor/cakephp/cakephp/src/Http/Runner.php(73): Cake\Routing\Middleware\RoutingMiddleware->process(Object(Cake\Http\ServerRequest), Object(Cake\Http\Runner))
#3 /var/www/html/vendor/cakephp/cakephp/src/Routing/Middleware/AssetMiddleware.php(68): Cake\Http\Runner->handle(Object(Cake\Http\ServerRequest))
#4 /var/www/html/vendor/cakephp/cakephp/src/Http/Runner.php(73): Cake\Routing\Middleware\AssetMiddleware->process(Object(Cake\Http\ServerRequest), Object(Cake\Http\Runner))
#5 /var/www/html/vendor/cakephp/cakephp/src/Error/Middleware/ErrorHandlerMiddleware.php(121): Cake\Http\Runner->handle(Object(Cake\Http\ServerRequest))
#6 /var/www/html/vendor/cakephp/cakephp/src/Http/Runner.php(73): Cake\Error\Middleware\ErrorHandlerMiddleware->process(Object(Cake\Http\ServerRequest), Object(Cake\Http\Runner))
#7 /var/www/html/vendor/cakephp/cakephp/src/Http/Runner.php(58): Cake\Http\Runner->handle(Object(Cake\Http\ServerRequest))
#8 /var/www/html/vendor/cakephp/cakephp/src/Http/Server.php(90): Cake\Http\Runner->run(Object(Cake\Http\MiddlewareQueue), Object(Cake\Http\ServerRequest), Object(App\Application))
#9 /var/www/html/vendor/cakephp/cakephp/src/TestSuite/MiddlewareDispatcher.php(190): Cake\Http\Server->run(Object(Cake\Http\ServerRequest))
#10 /var/www/html/vendor/cakephp/cakephp/src/TestSuite/IntegrationTestTrait.php(499): Cake\TestSuite\MiddlewareDispatcher->execute(Array)
#11 /var/www/html/vendor/cakephp/cakephp/src/TestSuite/IntegrationTestTrait.php(401): App\Test\TestCase\Controller\Api\EmployeesControllerTest->_sendRequest('/api/employees/...', 'POST', Array)
#12 /var/www/html/tests/TestCase/Controller/Api/EmployeesControllerTest.php(41): App\Test\TestCase\Controller\Api\EmployeesControllerTest->post('/api/employees/...', Array)
#13 /var/www/html/vendor/phpunit/phpunit/src/Framework/TestCase.php(1415): App\Test\TestCase\Controller\Api\EmployeesControllerTest->testJwtTokenPost()
#14 /var/www/html/vendor/phpunit/phpunit/src/Framework/TestCase.php(1035): PHPUnit\Framework\TestCase->runTest()
#15 /var/www/html/vendor/phpunit/phpunit/src/Framework/TestResult.php(691): PHPUnit\Framework\TestCase->runBare()
#16 /var/www/html/vendor/phpunit/phpunit/src/Framework/TestCase.php(763): PHPUnit\Framework\TestResult->run(Object(App\Test\TestCase\Controller\Api\EmployeesControllerTest))
#17 /var/www/html/vendor/phpunit/phpunit/src/Framework/TestSuite.php(597): PHPUnit\Framework\TestCase->run(Object(PHPUnit\Framework\TestResult))
#18 /var/www/html/vendor/phpunit/phpunit/src/TextUI/TestRunner.php(627): PHPUnit\Framework\TestSuite->run(Object(PHPUnit\Framework\TestResult))
#19 /var/www/html/vendor/phpunit/phpunit/src/TextUI/Command.php(204): PHPUnit\TextUI\TestRunner->doRun(Object(PHPUnit\Framework\TestSuite), Array, Array, true)
#20 /var/www/html/vendor/phpunit/phpunit/src/TextUI/Command.php(163): PHPUnit\TextUI\Command->run(Array, true)
#21 /var/www/html/vendor/phpunit/phpunit/phpunit(61): PHPUnit\TextUI\Command::main()
#22 {main}

and below is the token method

  public function token()
  {
    $this->getRequest()->allowMethod('post');

    $employee = $this->Employees
        ->find()
        ->select(['Employees.id','Employees.project_id', 'Employees.name'])
        ->where(['Employees.pin_code' => $this->request->getData('pin_code')])
        ->firstOrFail()
        ->toArray();

      $sites = TableRegistry::getTableLocator()->get('Sites');
      $currentSite = array('id' => 0, 'name'=> 0);
      $currentTransactionId = 0;
      $transactions = TableRegistry::getTableLocator()->get('Transactions');
      $previousTransaction = $transactions->find()->where([
        'Transactions.employee_id' => $employee['id']
      ])->order([
        'Transactions.created' => 'DESC'
      ])->first();
      if(!empty($previousTransaction) && empty($previousTransaction->photo_out)) {
        $currentSite = $sites->get($previousTransaction->site_id);
        $currentTransactionId = $previousTransaction->id;
      }
      $this->set([
        'success' => true,
        'employee' => $employee,
        'current_site' => $currentSite,
        'sites' => $sites->find()->matching('Projects', function ($q) use ($employee) {
          return $q->where(['Projects.id' => $employee["project_id"]]);
        }),
        'transaction_id' => $currentTransactionId,
        'data' => [
          'token' => JWT::encode([
              'sub' => $employee['id'],
              'exp' =>  time() + 604800
          ],
          Security::getSalt())
        ],
      ]);
      // Specify which view vars JsonView should serialize.
      $this->viewBuilder()->setOption('serialize', ['success', 'employee', 'current_site', 'sites', 'transaction_id', 'data']);
  }

and yes the middleware is set up in src/Application.php

public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue { $middlewareQueue

    // Catch any exceptions in the lower layers,
    // and make an error page/response
    ->add(new ErrorHandlerMiddleware(Configure::read('Error')))

    // Handle plugin/theme assets like CakePHP normally does.
    ->add(new AssetMiddleware([
        'cacheTime' => Configure::read('Asset.cacheTime'),
    ]))

    // Add routing middleware.
    // If you have a large number of routes connected, turning on routes
    // caching in production could improve performance. For that when
    // creating the middleware instance specify the cache config name by
    // using it's second constructor argument:
    // `new RoutingMiddleware($this, '_cake_routes_')`
    ->add(new RoutingMiddleware($this))

    // Parse various types of encoded request bodies so that they are
    // available as array through $request->getData()
    // https://book.cakephp.org/4/en/controllers/middleware.html#body-parser-middleware
    ->add(new BodyParserMiddleware())

    // Cross Site Request Forgery (CSRF) Protection Middleware
    // https://book.cakephp.org/4/en/controllers/middleware.html#cross-site-request-forgery-csrf-middleware
    // ->add(new CsrfProtectionMiddleware([
    //     'httponly' => false,
    // ]))

    // Add the middleware to the middleware queue
    ->add(new AuthenticationMiddleware($this));

return $middlewareQueue;

}

and below is the test method

  public function testJwtTokenPost() {
    $this->configRequest([
      'headers' => [
        'Accept' => 'application/json',
        'Content-Type' => 'application/json; charset=utf-8'
      ]
    ]);
    // $this->enableCsrfToken();
    $this->post('/api/employees/token', [
      'pin_code' => '1001'
    ]);
    $this->assertResponseOk();
    $this->assertResponseContains('success');
    $this->assertResponseContains('employee');
    $this->assertResponseContains('current_site');
    $this->assertResponseContains('transaction_id');
    $this->assertResponseContains('sites');
    $this->assertResponseContains('data');
  }

Below is my postman json call

在此处输入图片说明

When testing non- application/x-www-form-urlencoded requests you must pass the data as a string, as the test case can't reliably make assumptions about how to possibly transform the data into a string itself.

ie, pass a JSON string as POST data, not an array, a string is how application/json data would be exposed by PHP for an actual HTTP request:

$this->post('/api/employees/token', json_encode([
    'pin_code' => '1001'
]));

There's no need to further configure the body parser middleware for this, it supports JSON out of the box.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM