# Slim 3.6.0 release -

**URL:** <https://discourse.slimframework.com/t/slim-3-6-0-release/925>\
**Category:** Questions\
**Created:** [November 21, 2016, 3:02pm UTC](https://discourse.slimframework.com/t/slim-3-6-0-release/925 "2016-11-21T15:02:41Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![eranmalik](https://avatars.discourse-cdn.com/v4/letter/e/eb8c5e/32.png) [@eranmalik](https://discourse.slimframework.com/u/eranmalik)\
**Post date:** [November 21, 2016, 3:02pm UTC](https://discourse.slimframework.com/t/slim-3-6-0-release/925/1 "2016-11-21T15:02:41Z")

</div>

The Response’s withJson() fix is NOT working now, it return an empty response with status 200 OK ( on POST request).  
( I’m using the same code i used with 3.5.0, and it was OK):

return $response-\>withJson($resp, 200);

- $resp is an Array…

Is there something new we need to use?

Thanks.  
Malik Eran

---

<div class="post-metadata">

**Author:** ![ChristianDube](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@ChristianDube](https://discourse.slimframework.com/u/ChristianDube)\
**Post date:** [December 23, 2016, 2:12pm UTC](https://discourse.slimframework.com/t/slim-3-6-0-release/925/2 "2016-12-23T14:12:31Z")

</div>

I have the same problem but only when using multiple Middlewares.

A -\> B -\> C -\> B -\> A

If I try to use “return $response-\>withJson($result, 200);” from B or C the response body will be empty.

My code works fine against Slim v3.5 and breaks at v3.6

---

<div class="post-metadata">

**Author:** ![tflight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/tflight/32/1205_2.png) [@tflight](https://discourse.slimframework.com/u/tflight)\
**Post date:** [December 23, 2016, 6:51pm UTC](https://discourse.slimframework.com/t/slim-3-6-0-release/925/3 "2016-12-23T18:51:23Z")

</div>

Might it be because of this?

[Ensure withJson returns a new Response #1975](https://github.com/slimphp/Slim/pull/1975)

---

<div class="post-metadata">

**Author:** ![panic](https://avatars.discourse-cdn.com/v4/letter/p/3ab097/32.png) [@panic](https://discourse.slimframework.com/u/panic)\
**Post date:** [February 24, 2017, 3:06pm UTC](https://discourse.slimframework.com/t/slim-3-6-0-release/925/4 "2017-02-24T15:06:47Z")

</div>

I’ve just wanted to update from 3.5.0 to 3.7.0 and am experiencing the same behaviour (also with 3.6.0). Going back to 3.5.0 everything works fine again.

@tflight As no one got back here, could you please give a little explenation. I am not that code savvy. By the way I use php7.1.1 if that matters.

---

<div class="post-metadata">

**Author:** ![llvdl](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/llvdl/32/78_2.png) [@llvdl](https://discourse.slimframework.com/u/llvdl)\
**Post date:** [February 25, 2017, 9:26am UTC](https://discourse.slimframework.com/t/slim-3-6-0-release/925/5 "2017-02-25T09:26:00Z")

</div>

@panic, @ChristianDube

Can you give a minimal example? I can’t reproduce getting an empty response using `withJson`.

---

<div class="post-metadata">

**Author:** ![panic](https://avatars.discourse-cdn.com/v4/letter/p/3ab097/32.png) [@panic](https://discourse.slimframework.com/u/panic)\
**Post date:** [February 27, 2017, 7:12am UTC](https://discourse.slimframework.com/t/slim-3-6-0-release/925/6 "2017-02-27T07:12:36Z")

</div>

Hi @llvdl,

don’t know where to begin. Here is a code example and the middlewares:

```
use PDO;
use Psr\Log\LoggerInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;

class getItems {
	protected $logger;
	protected $db;
	protected $csrf;

	//Constructor
	public function __construct($csrf, PDO $db, LoggerInterface $logger) {
		$this->db = $db;
		$this->logger = $logger;
		$this->csrf = $csrf;
	}

	/**
	 * Returns all items from the database.
	 */
	public function index(Request $request, Response $response, $args) {
		$result = $this->db->query('SELECT * FROM items')->fetchAll(PDO::FETCH_ASSOC);
		$response->withJson($result, 200);
	}

```

Middleware:

```
class Middleware
{
    protected $container;

    public function __construct($container)
    {
        $this->container = $container;
    }
}

```

AuthMiddleware:

```
use Dflydev\FigCookies\FigResponseCookies;

class AuthMiddleware extends Middleware
{
    public function __invoke($request, $response, $next)
    {
        if (!$this->container->auth->isValid()) {
            $this->container->flash->addMessage('error', 'Please sign in before doing that.');

            $settings = $this->container->get('settings');
            $domain = $settings['jwt']['domain'];
            // Expire cookie
            $response = FigResponseCookies::expire($response, 'token', $domain, '/');
            return $response->withRedirect($this->container->router->pathFor('auth.login'));
        }

        $response = $next($request, $response);
        return $response;
    }
}

```

CsrfMiddleware:

```
class CsrfViewMiddleware extends Middleware
{
    public function __invoke($request, $response, $next)
    {
        $this->container->view->getEnvironment()->addGlobal('csrf', [
            'field' => '
                <input type="hidden" name="' . $this->container->csrf->getTokenNameKey() . '" value="' . $this->container->csrf->getTokenName() . '">
                <input type="hidden" name="' . $this->container->csrf->getTokenValueKey() . '" value="' . $this->container->csrf->getTokenValue() . '">
            ',
        ]);

        $response = $next($request, $response);
        return $response;
    }
}

```

GuestMiddleware:

```
class GuestMiddleware extends Middleware
{
    public function __invoke($request, $response, $next)
    {
        if ($this->container->auth->isValid()) {
            return $response->withRedirect($this->container->router->pathFor('home'));
        }

        $response = $next($request, $response);
        return $response;
    }
}
```

---

<div class="post-metadata">

**Author:** ![panic](https://avatars.discourse-cdn.com/v4/letter/p/3ab097/32.png) [@panic](https://discourse.slimframework.com/u/panic)\
**Post date:** [February 27, 2017, 8:30am UTC](https://discourse.slimframework.com/t/slim-3-6-0-release/925/7 "2017-02-27T08:30:34Z")

</div>

I’ve also noticed the response has a content type of “text/html; charset=UTF-8” instead of json.  
With 3.5 I’ve also have a content type of text/html but it works…

---

<div class="post-metadata">

**Author:** ![akrabat](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/akrabat/32/9_2.png) [@akrabat](https://discourse.slimframework.com/u/akrabat)\
**Post date:** [February 27, 2017, 9:25am UTC](https://discourse.slimframework.com/t/slim-3-6-0-release/925/8 "2017-02-27T09:25:14Z")

</div>

This happens when you don’t return the `$response` that you created via `withJson`.

i.e.

```auto
$app->get('/users', function ($request, $response) {
    $response->withJson(['user1'. 'user2']);
});

```

Will not return the JSON:

```auto
$ curl -i http:/localhost:8888/users
HTTP/1.1 200 OK
Host: localhost:8888
Connection: close
X-Powered-By: PHP/7.0.15
Content-Type: text/html; charset=UTF-8

$

```

However:

```auto
$app->get('/users', function ($request, $response) {
    return $response->withJson(['user1'. 'user2']);
});

```

will work:

```auto
$ curl -i http:/localhost:8888/users
HTTP/1.1 200 OK
Host: localhost:8888
Connection: close
X-Powered-By: PHP/7.0.15
Content-Type: application/json;charset=utf-8

["user1","user2"]

```

This is because PSR-7 is immutable and you get back a new Response object. As @tflight noted, there was a bug in earlier versions of Slim3 where it happened to work by accident if you didn’t return the `$response` object as we just modified the underlying stream.

---

<div class="post-metadata">

**Author:** ![akrabat](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/akrabat/32/9_2.png) [@akrabat](https://discourse.slimframework.com/u/akrabat)\
**Post date:** [February 27, 2017, 9:26am UTC](https://discourse.slimframework.com/t/slim-3-6-0-release/925/9 "2017-02-27T09:26:01Z")

</div>

The problem is here:

```auto
	public function index(Request $request, Response $response, $args) {
		$result = $this->db->query('SELECT * FROM items')->fetchAll(PDO::FETCH_ASSOC);
		$response->withJson($result, 200);
	}

```

You do not return the newly created Response that has the JSON in it.

Change to:

```auto
	public function index(Request $request, Response $response, $args) {
		$result = $this->db->query('SELECT * FROM items')->fetchAll(PDO::FETCH_ASSOC);
		return $response->withJson($result, 200);
	}

```

---

<div class="post-metadata">

**Author:** ![panic](https://avatars.discourse-cdn.com/v4/letter/p/3ab097/32.png) [@panic](https://discourse.slimframework.com/u/panic)\
**Post date:** [February 27, 2017, 10:01am UTC](https://discourse.slimframework.com/t/slim-3-6-0-release/925/10 "2017-02-27T10:01:57Z")

</div>

Oh boy… Thank you @akrabat!
