Status 200 always returned when setting status


#1

Hi,

First of all, great framework!

I’m struggling with this for a while. No matter what I do, it always returns status 200.

This is my code:

return $response->withJson($check, 500);

Also tried it like this:

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

Status 200 is always returned. I’m running it locally (osx, wamp, apache), don’t know if that’s an issue.

Thanks in advance and keep up the great work.


#2

Can you show us the controller/usecase who handle this request?


#3

This is the code, stripped out the other methods…

<?php

require(“vendor/autoload.php”);
include_once “…/…/DB.php”;

use \Psr\Http\Message\ServerRequestInterface;
use \Psr\Http\Message\ResponseInterface;

// Create App
$app = new \Slim\App;

// Make sure we can get the ip from the request
$app->add(new RKA\Middleware\IpAddress());

// Logging
error_reporting(E_ALL);
ini_set(‘display_errors’,‘Off’);
ini_set(‘error_log’,’./logs/errors.log’);

$app->post(’/generate’, function (ServerRequestInterface $request, ResponseInterface $response, $args) {

$check = checkRequestCount('/generate', $request->getAttribute('ip_address'));
if($check) {
	return $response->withJson($check, 500);
} else {

}

});

$app->run();


#4

The last else is empty :smiley:


#5

Haha, you don’t say :wink:

I stripped out that part, because it is inrelevant… It comes into the if for sure, it also returns the $check string, but with code 200 instead of 500.


#6

How are you making the client request to that route? What does your http access_log (or equivalent) say for the URI being accessed?


#7

Not that much, I only see this in the access log, is this what you mean?

::1 - - [11/Jul/2017:20:32:35 +0200] "POST /mooddii/api/generate HTTP/1.1" 200 15 ::1 - - [11/Jul/2017:20:32:42 +0200] "OPTIONS * HTTP/1.0" 200

#8

Found it, had a whitespace outside the php tag in one of the files. Deleted it and it works now!


#9

THANK YOU SOOO MUCH! I was about to loose my mind and it was only that single whitespace for me as well


#10

Wow, I was a bit skeptical about the whitespace solution but it was a whitespace. Seems like my Nginx was considering this as invalid script response. Make sense.

Thanks, also fixed my problem. Here’s is where I had the whitespace (well, whiteline)