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.94k stars 1.95k forks source link

Problem with status code and body #1766

Closed kristijandraca-xx closed 8 years ago

kristijandraca-xx commented 8 years ago

When I use this code

$response->getBody()->write('No user found'); 
return $response->withStatus(400);

My response is

No user fou

And when I switch to status code 200 everything is fine... How to fix this?

silentworks commented 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'); 
kristijandraca-xx commented 8 years ago

In this scenario status code is 200

tuupola commented 8 years ago

Code below seems to work.

return $response
    ->withStatus(400)
    ->write("No user found");
pabloroca commented 8 years ago

This works too.

$response->getBody () ->write (json_encode($result));
$responsenew = $response ->withStatus(400);
return $responsenew;
programster commented 8 years ago

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?

kristijandraca-xx commented 8 years ago

@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

kristijandraca-xx commented 8 years ago

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);
        });
    });
});
juliangut commented 8 years ago

$body->write returns number of bytes writen, not another modified body object

kristijandraca-xx commented 8 years ago

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

campersau commented 8 years ago

Maybe it is the same issue as in #1647 or #1743 ?

kristijandraca-xx commented 8 years ago

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

linebreak

akrabat commented 8 years ago

Where are the line breaks? Do you have characters before the opening <?php ?

kristijandraca-xx commented 8 years ago

No, I don't have

kristijandraca-xx commented 8 years ago

@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