14

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.

2
  • Please check Ur form tag with method post $postParam = $this->context->getRequest()->getParam('<param>');
    – Jackson
    Commented Dec 12, 2016 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
    Commented Feb 12, 2019 at 4:01

5 Answers 5

29

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;

}
2
3

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;

    }
}
2
2

Since Magento\Framework\App\Action\Action is now deprecated, this is the right way to do it :

<?php

namespace Spaarg\Api\Controller\Token;

use Magento\Framework\App\RequestInterface;

class Index implements HttpPostActionInterface
{

    /**
     * @var RequestInterface
     */
    protected $_request;

    /**
     * @param RequestInterface $request
     */
    public function __construct(
        RequestInterface $request,
    ) {
        $this->_request = $request;
    }

    public function execute()
    {
        $allParams = $this->_request->getParams();
        $oneParam = $this->_request->getParam('the_param_name');


    }
}

Note : implements HttpGetActionInterface for GET controllers.

1

This works very well for grabbing POST data in a controller:

$postData = $this->getRequest()->getContent();
0

In Magento 2.4.3 We can use the below code to get the post value in the controller.

namespace Module\Custom\Controller;

use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\Controller\ResultFactory;

class Index extends \Magento\Framework\App\Action\Action implements HttpPostActionInterface
{
    /**
     * @var Psr\Log\LoggerInterface
     */
    protected $logger;

    /**
     * @param Magento\Framework\App\Action\Context $context
     * @param Psr\Log\LoggerInterface $logger
     */
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Psr\Log\LoggerInterface $logger
    ) {
        parent::__construct($context);
        $this->logger = $logger;
    }

    /**
     * @return \Magento\Framework\Controller\ResultInterface
     */
    public function execute()
    {
        try {
            $allPostValues = $this->getRequest->getParams();
            $onePostValue = $this->getRequest->getParam('post_param');

            $this->logger->info($onePostValue);
            $this->logger->info(print_r($allPostValues,true));
            
            return $this->resultFactory->create(ResultFactory::TYPE_PAGE);

        } catch (\Exception $e) {
            $this->logger->error($e->getMessage());
        }
    }
}

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.