8

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.

  • Please check Ur form tag with method post $postParam = $this->context->getRequest()->getParam('<param>'); – Ankit Shah Dec 12 '16 at 1:41
  • @Anand, could you help on how did you implement your custom api endpoint and get post params using controller? Thanks in advance – fmsthird Feb 12 at 4:01
16

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;

}
  • If I want to use custom method for getting post parameters then how will it possible? – Pratik Mehta Jul 3 '18 at 6:27
  • 1
    I want to give 100 upvotes to this answer – Murtuza Zabuawala Oct 15 '18 at 13:10
2

If you want to get post data from controller,

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

Here your full code,

Also You have to declare storemanager object inside __construct() function of your php file instead of use dirctly objectmanager.

I have updated your code as below,

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,
        \Magento\Store\Model\StoreManagerInterface $storeManager
    ) {
        parent::__construct($context);
        $this->_cacheTypeList = $cacheTypeList;
        $this->_cacheState = $cacheState;
        $this->_cacheFrontendPool = $cacheFrontendPool;
        $this->resultPageFactory = $resultPageFactory;
        $this->storeManager = $storeManager;
    }

    /**
     * Flush cache storage
     *
     */
    public function execute()
    {
        $currentStore = $this->storeManager->getStore();
        $baseUrl = $currentStore->getBaseUrl();

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

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

    }
}
  • Is it possible to unset/remove post params i.e.$post = $this->getRequest()->getPostValue(); – Slimshadddyyy Nov 24 '17 at 6:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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