Closed kristijandraca-xx closed 8 years ago
Can you let me know if you get a different result if you chain all of those together?
return $response
->withStatus(400)
->getBody()->write('No user found');
In this scenario status code is 200
Code below seems to work.
return $response
->withStatus(400)
->write("No user found");
This works too.
$response->getBody () ->write (json_encode($result));
$responsenew = $response ->withStatus(400);
return $responsenew;
The response object is immutable. All methods (as far as I've seen) return a copy of the object that has modifications rather than altering the object itself. This is why one needs to either perform all the methods in a single chain like:
return $object->method1()->method2();
or one needs to use several variables:
$modifiedObject1 = $object->method1();
$modifiedObject2 = $modifiedObject1->method2();
return $modifiedObject2;
In this second case, $object, $modifiedObject1, and $modifiedObject2 are all different.
getBody is somewhat different because I believe it doesn't return the response object, but a copy of it's body.
I would try:
$body = $response->getBody();
$modifiedBody = $body->write('No user found');
return $response->withBody($modifiedBody)->withStatus(400);
Or maybe I've misunderstood the problem here?
@programster I try your example but I get
PHP Catchable fatal error: Argument 1 passed to Slim\Http\Message::withBody() must implement interface Psr\Http\Message\StreamInterface, integer given, called in .../index.php on line 29 and defined in .../vendor/slim/slim/Slim/Http/Message.php on line 287
here is full code
$app->group('/users', function () {
$this->group('/login', function () {
$this->get('/id', function (ServerRequestInterface $request, ResponseInterface $response) {
$body = $response->getBody();
$modifiedBody = $body->write('No user found');
return $response->withBody($modifiedBody)->withStatus(400);
});
});
});
$body->write returns number of bytes writen, not another modified body object
I change code to
$app->group('/users', function () {
$this->group('/login', function () {
$this->get('/id', function (ServerRequestInterface $request, ResponseInterface $response) {
$modifiedBody = $response->getBody();
$modifiedBody->write('No user found');
return $response->withBody($modifiedBody)->withStatus(400);
});
});
});
and my response is still same, missing last two char
No user fou
Maybe it is the same issue as in #1647 or #1743 ?
I think that I found problem.
I try this https://github.com/slimphp/Slim/issues/1743#issuecomment-175813194
'No user found' have 13 chars, and when I put:
$response = $response->withHeader('Content-Length', 14);
on more then needed I can see full text. But problem is that my response have linebreak before
Where are the line breaks? Do you have characters before the opening <?php
?
No, I don't have
@akrabat I create new index.php and rewrite whole thing, and it was blank space somewhere in the code. Tnx you all for your help
When I use this code
My response is
And when I switch to status code 200 everything is fine... How to fix this?