Fix Invalid Webservice Adapter Specified in Magento 1.9

apiexceptionmagento-1.9

I keep receiving this error in my exception log:

2016-10-14T09:20:43+00:00 ERR (3): 
exception 'Mage_Core_Exception' with message 'Invalid webservice adapter specified.' in /homepages/22/d123456/htdocs/mysite/app/Mage.php:595
Stack trace:
#0 /homepages/22/d123456/htdocs/mysite/app/code/core/Mage/Api/Model/Server.php(127): Mage::throwException('Invalid webserv...')
#1 /homepages/22/d123456/htdocs/mysite/api.php(84): Mage_Api_Model_Server->initialize(NULL)
#2 {main}

Also, this line is uncommented in my .htaccess:

###########################################
## rewrite API2 calls to api.php (by now it is REST only)

RewriteRule ^api/rest api.php?type=rest [QSA,L]

Best Answer

Your stack trace is showing that something odd is definitely afoot.

The key thing I'm seeing is that you are calling Mage_Api_Model_Server->initialize(NULL).

This function is defined like

public function initialize($adapterCode, $handler = null)
{
    /** @var $helper Mage_Api_Model_Config */
    $helper   = Mage::getSingleton('api/config');
    $adapters = $helper->getActiveAdapters();

    if (isset($adapters[$adapterCode])) {
        //Snipped for brevity, does adapter stuff
    } else {
        Mage::throwException(Mage::helper('api')->__('Invalid webservice adapter specified.'));
    }
    return $this;
}

If you pass in null, you are definitely not going to be selecting a valid adapter as $adapters[null] will not be a valid index key. I'm pretty sure null will be cast to the empty string in this instance, but there will not be any active adapters with the code of an empty string (or at least I hope not!)

The api.php file tries to call this initialise function like following (code edited for brevity).

//query parameter "type" is set by .htaccess rewrite rule
$apiAlias = Mage::app()->getRequest()->getParam('type');

/* @var $server Mage_Api_Model_Server */
$server = Mage::getSingleton('api/server');
$adapterCode = $server->getAdapterCodeByAlias($apiAlias);

// if no adapters found in aliases - find it by default, by code
if (null === $adapterCode) {
    $adapterCode = $apiAlias;
}
try {
    $server->initialize($adapterCode);

Based on your stack trace something is going wrong here and the final $adapterCode is ending up as null. The section of code which does the null === $adapterCode check will set the $adapterCode to be the value of your type parameter in the event that getAdapterCodeByAlias has returned null. This means that even if getAdapterCodeByAlias is returning null, it will ignore the value from that function so it can be disregarded from your issue.

The only way that I can see that $adapteCode will end up as null is if Mage::app()->getRequest()->getParam('type'); returns null. This is exactly how getParam is defined, it defaults to null if the parameter is missing.

public function getParam($key, $default = null)

All this boils down to the fact that you will likely be trying to access the api method without supplying a valid type parameter. This is an indication that your .htaccess file isn't configured properly. Try manually requesting with the type parameter, or get a systems admin to help figure out why the rewrite rules are not firing for you.

The valid types for me on a 1.14 magento instance are

echo implode(',', array_keys(Mage::getSingleton('api/config')->getActiveAdapters()))
#soap,soap_v2,soap_wsi,xmlrpc,default
Related Topic