Magento 2 API – How to Receive Post Parameters in Magento 2 Controller

apimagento2PHPrest

I am calling HTTP request for controller, I am getting get parameters, but I am not able to receive post parameters in Controller.

Basically I want to call Magento 2 APIs and send customized response to application, for that I have created a simple module, which will call API and customized response and send response to application,

But I am not able to fetch post parameters from request.

Here are some of my files which can give an idea about problem,

etc/webapi.xml

<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
    <route url="/V1/api/token" method="POST">
        <service class="Spaarg\Api\Api\apiInterface" method="token"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
    </route>
</routes>

Index.php

<?php
/**
 *
 * Copyright © 2015 Spaargcommerce. All rights reserved.
 */
namespace Spaarg\Api\Controller\Token;

class Index extends \Magento\Framework\App\Action\Action
{

    /**
     * @var \Magento\Framework\App\Cache\TypeListInterface
     */
    protected $_cacheTypeList;

    /**
     * @var \Magento\Framework\App\Cache\StateInterface
     */
    protected $_cacheState;

    /**
     * @var \Magento\Framework\App\Cache\Frontend\Pool
     */
    protected $_cacheFrontendPool;

    /**
     * @var \Magento\Framework\View\Result\PageFactory
     */
    protected $resultPageFactory;

    /**
     * @param Action\Context $context
     * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
     * @param \Magento\Framework\App\Cache\StateInterface $cacheState
     * @param \Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool
     * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
     */
    public function __construct(
       \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
        \Magento\Framework\App\Cache\StateInterface $cacheState,
        \Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory
    ) {
        parent::__construct($context);
        $this->_cacheTypeList = $cacheTypeList;
        $this->_cacheState = $cacheState;
        $this->_cacheFrontendPool = $cacheFrontendPool;
        $this->resultPageFactory = $resultPageFactory;
    }

    /**
     * Flush cache storage
     *
     */
    public function execute()
    {
        //$this->resultPage = $this->resultPageFactory->create();  
        //return $this->resultPage;

        $_objectManager = \Magento\Framework\App\ObjectManager::getInstance(); //instance of\Magento\Framework\App\ObjectManager
        $storeManager = $_objectManager->get('Magento\Store\Model\StoreManagerInterface'); 
        $currentStore = $storeManager->getStore();
        $baseUrl = $currentStore->getBaseUrl();

        $post = $this->getRequest()->getPost();

        echo "<pre>";
        print_r($post);
        exit;

    }
}

It will be great if someone can help.

Best Answer

To get Post data in controller you need to use following in your execute function.

public function execute()
{
    $post = $this->getRequest()->getPostValue();

    echo "<pre>";
    print_r($post);
    exit;

}
Related Topic