Response returning wrong status code


#1

This always returns 200 [Ok] despite the fact I set ->withStatus(400). Any advice on what I’ve screwed up here?

Thanks in advance!

// Get a single user by id
 11 $app->get('/user', function (Request $request, Response $response) {
 12     // Get the parameters
 13     $params = $request->getQueryParams();
 14     if (!isset($params['id']) || !is_numeric($params['id'])) {
 15         $newResponse = $response;
 16         $newResponse->withStatus(400)->write('Bad Request');
 17         return $newResponse;
 18     } ...

#2

withStatus returns a new instance of the response. Try:

$newResponse = $response->withStatus(400)->write('Bad Request');
return $newResponse;

#3

That fixed my issue, thank you so much!