Repository: codeigniter4/CodeIgniter4 (PHP)

Issue:
Bug: API\ResponseTrait::respondNoContent() returns Kint script in development mode
### PHP Version

7.4, 8.0, 8.1, 8.2

### CodeIgniter4 Version

4.1.5

### CodeIgniter4 Installation Method

Composer (using `codeigniter4/appstarter`)

### Which operating systems have you tested for this bug?

Windows, Linux

### Which server did you use?

cli-server (PHP built-in webserver)

### Database

_No response_

### What happened?

Postman return malformed response for respondNoContent(), because Kint active in development mode.

I have delete method in controller that return no content response `$this->respondNoContent()` in development mode, but when we access using postman it returns `Parse Error: The server returned a malformed response`. I try to find out why, in ReponseTrait.php turns out when $data null and $status is numeric then it's match with no content response, it skip the JSON formatter, the response is in text and debugger attach Kint toolbar so the postman says "malformed response". In production mode it works.

```php
protected function respond($data = null, ?int $status = null, string $message = '')
    {
        if ($data === null && $status === null) {
            $status = 404;
            $output = null;
        } elseif ($data === null && is_numeric($status)) {
            $output = null;
        } else {
            $status = empty($status) ? 200 : $status;
            $output = $this->format($data);
        }

        if ($output !== null) {
            if ($this->format === 'json') {
                return $this->response->setJSON($output)->setStatusCode($status, $message);
            }

            if ($this->format === 'xml') {
                return $this->response->setXML($output)->setStatusCode($status, $message);
            }
        }

        return $this->response->setBody($output)->setStatusCode($status, $message);
    }
```

### Steps to Reproduce

Create a controller:

```php
class Activities extends BaseController
{
    use ResponseTrait;

    public function delete($id)
    {
        $this->authorize(PERMISSION_ACTIVITY_DELETE);

        if ($this->activityService->delete($id)) {
            return $this->respondNoContent();
        }

        return $this->respond([
            'code' => 500,
            'status' => 'SERVER_ERROR',
            'errors' => 'Delete activity failed.'
        ], $code);
    }
}
```

use postman or other rest clients to hit the endpoint `DELETE /activities/1`

### Expected Output

Return response 204 No content

### Anything else?

_No response_


Discussion:
Thank you for reporting.

Try:
```diff
--- a/system/API/ResponseTrait.php
+++ b/system/API/ResponseTrait.php
@@ -92,8 +92,10 @@ trait ResponseTrait
         if ($data === null && $status === null) {
             $status = 404;
             $output = null;
+            $this->format($data);
         } elseif ($data === null && is_numeric($status)) {
             $output = null;
+            $this->format($data);
         } else {
             $status = empty($status) ? 200 : $status;
             $output = $this->format($data);
```
Should I create PR? because I can't edit the file directly in vendor directory right? @kenjis 
PRs are welcome.
https://github.com/codeigniter4/CodeIgniter4/blob/develop/contributing/pull_request.md

> I can't edit the file directly in vendor directory

You should not edit, because all files in vendor directory are manged by Composer,
and any changes will be removed when updating the package.






Patch:
diff --git a/system/API/ResponseTrait.php b/system/API/ResponseTrait.php
index 0b357f0a177b..9e8bad775f78 100644
--- a/system/API/ResponseTrait.php
+++ b/system/API/ResponseTrait.php
@@ -92,8 +92,10 @@ protected function respond($data = null, ?int $status = null, string $message =
         if ($data === null && $status === null) {
             $status = 404;
             $output = null;
+            $this->format($data);
         } elseif ($data === null && is_numeric($status)) {
             $output = null;
+            $this->format($data);
         } else {
             $status = empty($status) ? 200 : $status;
             $output = $this->format($data);


Test patch:
diff --git a/tests/system/API/ResponseTraitTest.php b/tests/system/API/ResponseTraitTest.php
index 0c060691b0b3..653cac115f21 100644
--- a/tests/system/API/ResponseTraitTest.php
+++ b/tests/system/API/ResponseTraitTest.php
@@ -331,6 +331,7 @@ public function testNoContent()
 
         $this->invoke($controller, 'respondNoContent', ['']);
 
+        $this->assertStringStartsWith('application/json', $this->response->getHeaderLine('Content-Type'));
         $this->assertSame('No Content', $this->response->getReason());
         $this->assertSame(204, $this->response->getStatusCode());
     }