Description
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.
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:
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