slimphp / Slim

Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs.
http://slimframework.com
MIT License
11.98k stars 1.95k forks source link

Response withStatus not working #3140

Closed met68 closed 2 years ago

met68 commented 2 years ago

Am trying to send a response as follows in a REST API and based on Slim documentation:

$response->getBody()->write(json_encode(["Error" => "HTTP STATUS 403 - FORBIDDEN!"])); return $response->withHeader('Content-Type', 'application/json')->withStatus(403);

Using Postman and Chrome browser to test the API, I always get the correct response body in JSON, but the status code is always 200 instead of the intended 403.

Have looked through previous open and closed issues, but found nothing which would help me.

Any help with this would therefore be much appreciated!

Using Slim v4 and PHP 7.4 on an Apache2 server.

l0gicgate commented 2 years ago

This must have to do with your apache configuration or something else.

Would you be able to provide an example repository with minimal code to reproduce?

met68 commented 2 years ago

Pierre, thanks for your swift reply. The server is a Digital Ocean Droplet using Ubuntu 18.something from memory and I haven't added any custom configuration, other than the fact that I'm using basic auth for authorisation, but that should not impact my responses and apart from the status codes not working, the API functions flawlessly otherwise.

Will see whether I can provide a skeleton repo so you can try and reproduce the error.

met68 commented 2 years ago

Ok, I just tried exactly the above code in an other Slim PHP v4 REST API which I am running on my local machine and alas the status code works fine here.

Curious. Will investigate my DO Droplet config and see what may be causing this issue.

Could it maybe be CORS related?

Will get back if/when I have figured-out the issue.

In the meantime, please feel free to close the issue, as it doesn't look to be directly related to Slim PHP.

met68 commented 2 years ago

Problem seems related to my before middleware. Will have to go over this again. In the meantime, I will close the issue, as It's unrelated to Slim PHP.