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.

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

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.

| improve this answer | |
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;
}
| improve this answer | |
  • But here the problem is: there are some REST APIs which are called from the website also. – MagePsycho Mar 2 '17 at 11:25
0

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.

| improve this answer | |
  • Yeah. This won't help me :( – MagePsycho Mar 2 '17 at 8:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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