Search code examples
phpslimslim-3

Slim 3 Save Request & Response In Database


I have created a middleware in Slim 3 which saves the request into the database.But i am having difficulty in saving the response. I am not getting the response in the middleware after the app runs. Please check the below code

$this->logId = $apiLogController->saveLog($parsedBody, $headers, $queryParams, $ipAddress, $url);
    //call the app
    $response->getBody()->write('BEFORE');
    $response = $next($request, $response);
    $response->getBody()->write('AFTER');
    echo '<pre>';
    var_dump($response->getBody()->getContents());exit;

I am also using other middlewares in the app. How can i get the response in the middleware so that i can save that in database.


Solution

  • Sorry I got the answer to my question after R&D. I need to rewind the body stream to get the contents.

    var_dump($response->getBody()->rewind()); 
    var_dump($response->getBody()->getContents());
    exit;