Welcome Guest, Not a member yet? Register   Sign In
set404Override - JSON not working
#1

I'm creating a RESTFUl API with JWT. The allowed method is POST, which I've setup in Routes.php. So when end use request with other than "POST" method will end up as 404. I have tried to changed in Routes.php as follows:

PHP Code:
$routes->set404Override('App\Controllers\ErrorController::index'); 
and this is my Controller:

Code:
<?php

namespace App\Controllers;

use App\Controllers\BaseController;
use CodeIgniter\HTTP\ResponseInterface;
use Config\Services;

class ErrorController extends BaseController
{
    public function index()
    {
        return Services::response()
            ->setJSON([
                'status_code' => ResponseInterface::HTTP_NOT_FOUND,
                'status_message' => 'Unknown method'
            ]);
    }
}

But I test in POSTMAN, it return empty or blank. How can I return JSON in custom 404?
Reply
#2

(This post was last modified: 02-15-2022, 08:37 AM by iRedds. Edit Reason: issue )

I think it's a bug.
Until the bug is fixed, you can use this solution.

PHP Code:
public function index()
{
    
Services::response()->setHeader('Content-Type''application/json');

    echo 
json_encode([
        
'status_code' => ResponseInterface::HTTP_NOT_FOUND,
        
'status_message' => 'Unknown method'
    
]);


https://github.com/codeigniter4/CodeIgni...ssues/5697
Reply
#3

Thanks @iRedds will give it a try.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB