Magento – Magento 2 Custom REST API – Error 400 – Specified request cannot be processed

magento2magento2.3

I've setup a REST API using the following configuration however when I call the endpoint with {base_url}/rest/V1/Company/Product/getProducts using a GET in postman I get a 400 error return with a body of

{"message": "Specified request cannot be processed."}

I have nothing logged in any of the var/log files (Including my specific log statement. Nothing logged in NGINX or FPM logs.

The following is the only place I can find that error.

vendor/magento/module-webapi/Controller/Rest/RequestProcessorPool.php

/**
 * {@inheritdoc}
 *
 * @throws \Magento\Framework\Webapi\Exception
 * return RequestProcessorInterface
 */
public function getProcessor(\Magento\Framework\Webapi\Rest\Request $request)
{
    foreach ($this->requestProcessors as $processor) {
        if ($processor->canProcess($request)) {
            return $processor;
        }
    }

    throw new \Magento\Framework\Webapi\Exception(
    __('Specified request cannot be processed.'),
    0,
    \Magento\Framework\Webapi\Exception::HTTP_BAD_REQUEST
    );
}

If it is that, its saying it can't find a "Processor", but whats that?

The following are my setup.

/app/code/Company/Product/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Company_Product',
    __DIR__
);

/app/code/Company/Product/etc/module.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Company_Product" setup_version="1.0.8" />
</config>

/app/code/Company/Product/etc/webapi.xml

<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
    <route url="/v1/Company/Product" method="GET">
    <service class="Company\Product\Api\ProductInterface" method="getProduct" />
    <resources>
        <resource ref="anonymous"/>
    </resources>
    </route>
</routes>

/app/code/Company/Product/etc/di.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Company\Product\Api\ProductInterface" type="Company\Product\Model\ProductManagement" />
</config>

/app/code/Company/Product/Api/ProductInterface.php

<?php
namespace Company\Product\Api;

/**
 * @api
 */
interface ProductInterface
{
    /**
     * Get recommended products based on another product
     *
     * @return int Some number
     */
    public function getProduct();
}

/app/code/Company/Product/Data/ProductManagement.php

<?php
namespace Company\Product\Model;

use Psr\Log\LoggerInterface;
use Company\Product\Api\ProductInterface;

class ProductManagement implements ProductInterface
{
    protected $_logger;

    /**
     *
     * @param \Psr\Log\LoggerInterface $logger
     */
    public function __construct
    (
        LoggerInterface $logger
    ) 
    {
        $this->_logger = $logger;
    }

    /**
     * @return int Some number
     */
    public function getProduct()
    {
        $this->_logger->debug("--> getProduct <--");

        return 12;
    }
}

Best Answer

So the issue came down to a simple issue of me misunderstanding what the URL to call was {base_url}/rest/V1/Company/Product/getProducts should have been {base_url}/rest/V1/Company/Product (Hindsight is awesome!).

Related Topic