In Magento 1, We used to check as:
if (Mage::getSingleton('api/server')->getAdapter() != null) {
// request from Web-Service
}
How to check the same in Magento2?
I didn't find any proper adapter which fits for both REST & SOAP.
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityIn Magento 1, We used to check as:
if (Mage::getSingleton('api/server')->getAdapter() != null) {
// request from Web-Service
}
How to check the same in Magento2?
I didn't find any proper adapter which fits for both REST & SOAP.
I used Magento\Framework\App\State::getAreaCode()
to find if request is coming via SOAP / REST API.
You can check the possible area codes in Magento\Framework\App\Area
class.
I had a similar task once, and if I remember correctly I did this by checking if \Magento\Framework\App\Request\Http::getFullActionName()
equals __
. I don't know why, but for REST requests this would be the full action name.
However, I did not find this safe at the time because it didn't feel like the most correct solution, so I ended up by strictly checking the request string:
if (
$this->request->getRequestString() === '/rest/V1/carts/mine/payment-information'
) {
...
As you might have guessed, in my situation I had to check if the current REST request was a very specific one.
I don't know about SOAP, but I'm assuming you could use a similar approach. However, it still does not quite feel like the proper solution.
Please check with below code,
public function __construct(\Magento\Framework\Event\Observer $observer) {
$controller = $observer->getControllerAction();
}
$isApirequest = $controller->getRequest()->getControllerModule() == 'Mage_Api';
if ($isApirequest) {
return;
}