Magento 2: Return JSON, XML, HTML & Raw Text Data Response from Controller

This article shows how you can return JSON data, XML data, HTML data, or Raw Text data from any Controller class in Magento 2.

Generally, in the Magento2 controller’s execute() function, we see the redirect or forwarding call which redirects to the specified URL page.

However, we might sometime need to return the JSON format data or even the raw text data.

Here, I will show how you can achieve this. I suppose the module name to be “Chapagain_HelloWorld“.

Return JSON data from Magento2 Controller

File: app/code/Chapagain/HelloWorld/Controller/Index/Index.php


<?php
namespace Chapagain\HelloWorld\Controller\Index;

use Magento\Framework\UrlFactory;

class Index extends \Magento\Framework\App\Action\Action
{
        /**
         * @var \Magento\Framework\View\Result\PageFactory
         */
        protected $resultPageFactory;

        /**
         * @var \Magento\Framework\UrlFactory
         */
        protected $urlFactory;
       
        /**
         * @var \Magento\Framework\Controller\Result\JsonFactory
         */
        protected $resultJsonFactory;
       
        /**
         * @param \Magento\Framework\App\Action\Context $context
         * @param \Magento\Framework\View\Result\PageFactory resultPageFactory
         * @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
         */
        public function __construct(
            \Magento\Framework\App\Action\Context $context,
            \Magento\Framework\View\Result\PageFactory $resultPageFactory,
            \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
            UrlFactory $urlFactory
        )
        {
            $this->resultPageFactory    = $resultPageFactory;
            $this->resultJsonFactory    = $resultJsonFactory;
            $this->urlModel             = $urlFactory->create();

            parent::__construct($context);
        }
    /**
     * Example for returning JSON data
     *
     * @return string
     */
    public function execute()
    {
        $result = $this->resultJsonFactory->create();

        $data = [
            'foo' =>'bar',
            'success' => true
        ];

        $result->setData($data);

        return $result;
    }
}
?>

Output:


{"foo":"bar","success":true}

Return XML data from Magento2 Controller

File: app/code/Chapagain/HelloWorld/Controller/Index/Index.php


<?php
namespace Chapagain\HelloWorld\Controller\Index;

use Magento\Framework\UrlFactory;

class Index extends \Magento\Framework\App\Action\Action
{
        /**
         * @var \Magento\Framework\View\Result\PageFactory
         */
        protected $resultPageFactory;

        /**
         * @var \Magento\Framework\UrlFactory
         */
        protected $urlFactory;
       
        /**
         * @var \Magento\Framework\Controller\Result\RawFactory
         */
        protected $resultRawFactory;
       
        /**
         * @param \Magento\Framework\App\Action\Context $context
         * @param \Magento\Framework\View\Result\PageFactory resultPageFactory
         * @param \Magento\Framework\Controller\Result\RawFactory $resultRawFactory
         */
        public function __construct(
            \Magento\Framework\App\Action\Context $context,
            \Magento\Framework\View\Result\PageFactory $resultPageFactory,
            \Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
            UrlFactory $urlFactory
        )
        {
            $this->resultPageFactory    = $resultPageFactory;
            $this->resultRawFactory     = $resultRawFactory;
            $this->urlModel             = $urlFactory->create();

            parent::__construct($context);
        }
    /**
     * Example for returning Raw Text data
     *
     * @return string
     */
    public function execute()
    {
        $result = $this->resultRawFactory->create();

        // Return XML data
        $result->setHeader('Content-Type', 'text/xml');
        $result->setContents('<user>
                                <name>Mukesh Chapagain</name>
                                <country>Nepal</country>
                            </user>');

        return $result;
    }
}
?>

Output:


<user>
    <name>Mukesh Chapagain</name>
    <country>Nepal</country>
</user>

Return Raw Text or HTML data from Magento2 Controller

File: app/code/Chapagain/HelloWorld/Controller/Index/Index.php


<?php
namespace Chapagain\HelloWorld\Controller\Index;

use Magento\Framework\UrlFactory;

class Index extends \Magento\Framework\App\Action\Action
{
        /**
         * @var \Magento\Framework\View\Result\PageFactory
         */
        protected $resultPageFactory;

        /**
         * @var \Magento\Framework\UrlFactory
         */
        protected $urlFactory;
       
        /**
         * @var \Magento\Framework\Controller\Result\RawFactory
         */
        protected $resultRawFactory;
       
        /**
         * @param \Magento\Framework\App\Action\Context $context
         * @param \Magento\Framework\View\Result\PageFactory resultPageFactory
         * @param \Magento\Framework\Controller\Result\RawFactory $resultRawFactory
         */
        public function __construct(
            \Magento\Framework\App\Action\Context $context,
            \Magento\Framework\View\Result\PageFactory $resultPageFactory,
            \Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
            UrlFactory $urlFactory
        )
        {
            $this->resultPageFactory    = $resultPageFactory;
            $this->resultRawFactory     = $resultRawFactory;
            $this->urlModel             = $urlFactory->create();

            parent::__construct($context);
        }
    /**
     * Example for returning Raw Text data
     *
     * @return string
     */
    public function execute()
    {
        $result = $this->resultRawFactory->create();

        // Return Raw Text or HTML data
        // $result->setContents('Hello World');
        $result->setContents('<strong>Hello World</strong>');

        return $result;
    }
}
?>

Hope this helps. Thanks.