# Filter response with Middleware

**URL:** <https://discourse.slimframework.com/t/filter-response-with-middleware/245>\
**Category:** Questions\
**Created:** [May 12, 2016, 8:42pm UTC](https://discourse.slimframework.com/t/filter-response-with-middleware/245 "2016-05-12T20:42:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![TomVW](https://avatars.discourse-cdn.com/v4/letter/t/f05b48/32.png) [@TomVW](https://discourse.slimframework.com/u/TomVW)\
**Post date:** [May 12, 2016, 8:42pm UTC](https://discourse.slimframework.com/t/filter-response-with-middleware/245/1 "2016-05-12T20:42:57Z")

</div>

Hello,

I’ve got a class with optional properties, when using json\_encode, these are encoded as ‘null’ when they have no value in the object.  
I figured that with Middleware I could filter out the null values.  
However i can’t seem to retrieve the raw JSON data of my body.  
This is my code so far:

```
$app->add(function ($request, $response, $next) {
	$response = $next($request, $response);

	if($response->getHeaderLine('Content-type') == 'application/json;charset=utf-8'){
		$content =$response->getBody();
		$response->getBody()->rewind();
		$response->getBody()->write(preg_replace('/,\s*"[^"]+":null|"[^"]+":null,?/', '', $content));

		return $response;
	}

	return $response;
});

```

It seems the getBody() call returns more than just the body: {actual body} [] {“uid”:“acf9546”}

Is there a way to apply this filter?

---

<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:** [May 13, 2016, 6:54am UTC](https://discourse.slimframework.com/t/filter-response-with-middleware/245/2 "2016-05-13T06:54:12Z")

</div>

There are two things going on here.

Firstly, $content is an instance of `Slim\Http\Stream` and so the you use it within the preg\_replace(), it will call `__toString()` resulting in a rewind followed by a play of the stream. Hence your filter appends to the end of the current data.

To fix this:

```auto
    $content = (string)$response->getBody();

```

The second issue is that when you write to the stream having rewound, the length of data in the stream is the same as it was before. i.e. you will have rogue data at the end.

To fix this you need to replace the Response’s body with your own. Hence your filter needs to look like this:

```auto
$app->add(function ($request, $response, $next) {
    $response = $next($request, $response);

    if ($response->getHeaderLine('Content-type') == 'application/json;charset=utf-8') {
        $content = (string)$response->getBody();
        $newBody = new \Slim\Http\Body(fopen('php://temp', 'r+'));
        $newBody->write(preg_replace('/,\s*"[^"]+":null|"[^"]+":null,?/', '', $content));
        $response = $response->withBody($newBody);
    }

    return $response;
});

```

---

<div class="post-metadata">

**Author:** ![TomVW](https://avatars.discourse-cdn.com/v4/letter/t/f05b48/32.png) [@TomVW](https://discourse.slimframework.com/u/TomVW)\
**Post date:** [May 13, 2016, 7:24am UTC](https://discourse.slimframework.com/t/filter-response-with-middleware/245/3 "2016-05-13T07:24:24Z")

</div>

My hat’s off to you, that works 🙂

Maybe this is something to add in the User Guide.
