Delete response body content


#1

Is there a way to delete the entire response body content, after it has been written to it?
Like this.

Stream.php

public function truncate() {
    ftruncate($this->stream, 0);
}

#2

probably by replacing with an empty PSR-7 valid object. Check out this from the docs:

You can also replace the PSR 7 Response object’s body with an entirely new StreamInterface instance.

An example:

$newStream = new \GuzzleHttp\Psr7\LazyOpenStream('/path/to/file', 'r');
$newResponse = $oldResponse->withBody($newStream);

Source


#3

Look promising.
But in case I would prefer to create an empty file stream from memory and an build in solution would be nice, too?

Anyway my slim skeleton implementation does not seem to come with Guzzle, do I have to download it from github separately?


#4

Easy way in Slim Framework 3. This works fine for me.

public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $args = null)
{
    // Get response set by middleware.
    $mw_response = $response->getBody()->__toString();

    // Now reset the response.
    $response = new \Slim\Http\Response();

    .....
}