0

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;
    }
}

2 Answers 2

1

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!).

1
  • 3
    I will add 1 more thing to watch out for here and that is that the "V1" is case sensitive. You cannot make your Route Url "v1" for eg, it must be a capital V in webapi.xml or else it will fail. Commented Nov 17, 2019 at 22:48
0

Please set Headers in postman.

Click on Headers in Postman.

Add Key: Authorization and Value: Bearer <Consumer Secret>

Key: Content-Type and Value: application/json

NOTE: Replace <Consumer Secret> with Magento Consumer Secret. Path: System -> Integrations -> Integration Details -> Consumer Secret

2
  • Why should that be necessary? The Webapi.xml has the resource set to anonymous. Commented Nov 13, 2019 at 20:16
  • So irrespective I added a bearer token as you suggested and nothing different. Commented Nov 14, 2019 at 0:37

Your Answer

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

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