The question:
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="SpaargApiApiapiInterface" method="token"/>
<resources>
<resource ref="anonymous"/>
</resources>
</route>
</routes>
Index.php
<?php
/**
*
* Copyright © 2015 Spaargcommerce. All rights reserved.
*/
namespace SpaargApiControllerToken;
class Index extends MagentoFrameworkAppActionAction
{
/**
* @var MagentoFrameworkAppCacheTypeListInterface
*/
protected $_cacheTypeList;
/**
* @var MagentoFrameworkAppCacheStateInterface
*/
protected $_cacheState;
/**
* @var MagentoFrameworkAppCacheFrontendPool
*/
protected $_cacheFrontendPool;
/**
* @var MagentoFrameworkViewResultPageFactory
*/
protected $resultPageFactory;
/**
* @param ActionContext $context
* @param MagentoFrameworkAppCacheTypeListInterface $cacheTypeList
* @param MagentoFrameworkAppCacheStateInterface $cacheState
* @param MagentoFrameworkAppCacheFrontendPool $cacheFrontendPool
* @param MagentoFrameworkViewResultPageFactory $resultPageFactory
*/
public function __construct(
MagentoFrameworkAppActionContext $context,
MagentoFrameworkAppCacheTypeListInterface $cacheTypeList,
MagentoFrameworkAppCacheStateInterface $cacheState,
MagentoFrameworkAppCacheFrontendPool $cacheFrontendPool,
MagentoFrameworkViewResultPageFactory $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 = MagentoFrameworkAppObjectManager::getInstance(); //instance ofMagentoFrameworkAppObjectManager
$storeManager = $_objectManager->get('MagentoStoreModelStoreManagerInterface');
$currentStore = $storeManager->getStore();
$baseUrl = $currentStore->getBaseUrl();
$post = $this->getRequest()->getPost();
echo "<pre>";
print_r($post);
exit;
}
}
It will be great if someone can help.
The Solutions:
Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.
Method 1
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;
}
Method 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 MagentoFrameworkAppActionAction
{
/**
* @var MagentoFrameworkAppCacheTypeListInterface
*/
protected $_cacheTypeList;
/**
* @var MagentoFrameworkAppCacheStateInterface
*/
protected $_cacheState;
/**
* @var MagentoFrameworkAppCacheFrontendPool
*/
protected $_cacheFrontendPool;
/**
* @var MagentoFrameworkViewResultPageFactory
*/
protected $resultPageFactory;
/**
* @param ActionContext $context
* @param MagentoFrameworkAppCacheTypeListInterface $cacheTypeList
* @param MagentoFrameworkAppCacheStateInterface $cacheState
* @param MagentoFrameworkAppCacheFrontendPool $cacheFrontendPool
* @param MagentoFrameworkViewResultPageFactory $resultPageFactory
*/
public function __construct(
MagentoFrameworkAppActionContext $context,
MagentoFrameworkAppCacheTypeListInterface $cacheTypeList,
MagentoFrameworkAppCacheStateInterface $cacheState,
MagentoFrameworkAppCacheFrontendPool $cacheFrontendPool,
MagentoFrameworkViewResultPageFactory $resultPageFactory,
MagentoStoreModelStoreManagerInterface $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;
}
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0