In Magento 1 I can do the following:

$response = $observer->getEvent()->getControllerAction()->getResponse();
if(!$response) {
   return $this;

$html = $response->getBody(); 

How can I do the same in Magento 2?

1 Answer 1


Assuming your module is under Vendor\Module

If you want to captures the HTML from the response, you can declare your event under etc\<area>\events.xml (where <area> is adminhtml or frontend ):

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="controller_front_send_response_before">
        <observer name="vendor_module" instance="Vendor\Module\Model\Observer" shared="false" />

Then under Model\Observer.php:


namespace Vendor\Module\Model;

use Magento\Framework\Event\ObserverInterface;

class Observer implements ObserverInterface
    public function execute(\Magento\Framework\Event\Observer $observer) 
        $response = $observer->getEvent()->getData('response');
        if (!$response) return;
        $body = $response->getBody();
  • Yes it's working fine thx right now i am not able to give you +50 because it's not completed 24 hours.
    – Pratik
    Commented Apr 1, 2016 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.