8

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.

2
  • are you looking for observer? Mar 2, 2017 at 8:43
  • Mostly this will be checked from Observer.
    – MagePsycho
    Mar 2, 2017 at 9:24

3 Answers 3

10

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.

2

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.

1
  • Yeah. This won't help me :(
    – MagePsycho
    Mar 2, 2017 at 8:40
1

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;
}
1
  • 1
    But here the problem is: there are some REST APIs which are called from the website also.
    – MagePsycho
    Mar 2, 2017 at 11:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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