Magento2: How to check if the request is from Web Service or Magento 2 check request from website or API
Today we will share an idea to identify the request is coming from API or website.
Sometimes need a condition to check the page called for the mobile app user to the browser user.
The code level to get area code in your code like frontend,adminhtml, webapi_rest,webapi_soap etc.
Also, check using getFullActionName of the current page action.
There are two ways to check URL hitting by mobile app or API and browser.
1. Method: using area code
protected $state; public function __construct( \Magento\Framework\App\State $state ) { $this->state = $state; } public function getArea() { echo $this->state->getAreaCode(); } // using object manager $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $area = $objectManager->get('Magento\Framework\App\State'); $areaCode = $area->getAreaCode();
2. Method: using action name
protected $request; public function __construct( \Magento\Framework\App\Request\Http $request, ) { $this->request = $request; } public function getAction(){ echo $fullActionName = $this->request->getFullActionName(); } // using object manager $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $request = $objectManager->get('\Magento\Framework\App\Request\Http'); echo $fullActionName = $request->getFullActionName();
Magento 2 check request from website or API
Read below related blogs : Magento 2 speed