-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't create 200 OK response with Location header #1730
Comments
HTTP Location is valid with: it shouldn't be send with 200. |
It might be PHP adding that status code once it detects the location header - I tried looking in the source but couldn't see anywhere obvious where the status code would be changed. |
@ppetermann I know, that's why I've mentioned the case for client that does not recognize 201 response properly. @AndrewCarterUK I also haven't found it in Slim code. |
@adambro: use 3xx status code then. |
@ppetermann thanks for hint, but 3xx is not really an option - it's interpreted as error by the client. Unfortunately I can't fix that. I'd rather get rid of |
@adambro can you dump the response object before it gets sent? You should be able to run Slim in silent mode to prevent it actually sending the response: $response = $app->run(true);
var_dump($response); |
what sort of client is that? the 300's are pretty common status codes |
@ppetermann it's an embedded software system. @AndrewCarterUK here's the dump of response:
|
I don't think Slim is your problem here. Maybe try running a file like this: // test.php
header('HTTP/1.1 200 Ok');
header('Location: /my-redirect');
echo 'Hello, World!'; And check what status code you actually get. If you get a 302 from that, you should look into PHP and webserver configuration. |
Indeed, I was able to reproduce the issue using @AndrewCarterUK code above. I've got I'm using Apache 2.4 from Docker image |
This is a PHP feature. From the header manual page:
|
Seems like we could work around that behavior though, based on: So the PSR-7 message format starts doing what you'd expect it to do. Basically you'd have to treat the Location header uniquely, passing the response code along with the redirect. Just checked RFC 7231: https://tools.ietf.org/html/rfc7231, and while it mentions 3xx and 201 explicitly, it also doesn't say that other status codes can't be used with the Location header. See: https://tools.ietf.org/html/rfc7231#section-7.1.2. So this is a case of the runtime being overly "helpful", removing fidelity from the PSR-7 representation. |
It's rather a bug than a feature. ACME is one example that requires sending a It should at least be ignored if an explicit status code is set, which seems to be handled when looking at the code @iansltx linked. |
FWIW this also works: <?php header('Location: /foo'); http_response_code(202); So revising App::respond() to include an explicit http_response_code() call at the end should clean this up, if we don't want to start handling individual headers differently. |
PHP thinks it's clever, but it isn't. Fixes slimphp#1730.
See #2309 for a PR. |
PHP thinks it's clever, but it isn't. Fixes slimphp#1730.
PHP thinks it's clever, but it isn't. Fixes #1730.
See the subsequent #2345. |
I'd like to respond
200 OK
withLocation
HTTP header, but what I'm getting is302 Found
instead:The same happens when I try to use
204 No Content
response.Surprisingly
201 Created
works as expected. Can we make it work for 200 as well for clients that do not know how to handle 201, please?The text was updated successfully, but these errors were encountered: