cancel
Showing results for 
Search instead for 
Did you mean: 

Custom REST API - Error 400 - Specified request cannot be processed

SOLVED

Custom REST API - Error 400 - Specified request cannot be processed

Hi all, I am cross posting from SE as I am not getting any response there.

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 is 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;
    }
}
1 ACCEPTED SOLUTION

Accepted Solutions

Re: Custom REST API - Error 400 - Specified request cannot be processed

So for posterity the issue was a misunderstanding on the route url in webapi.xml. 

I thought the method in the service element was appended to the url in route element (NFI why i thought that!).

So {base_url}/rest/V1/Company/Product/getProducts should have been {base_url}/rest/V1/Company/Product.

 

View solution in original post

4 REPLIES 4

Re: Custom REST API - Error 400 - Specified request cannot be processed

Hello @drw_ 

Can you try this?

Change the options request to 200 ok

RewriteCond %{REQUEST_METHOD} OPTIONS RewriteRule ^(.*)$ $1 [R=200,L]

<IfModule mod_headers.c>
    Header always set Access-Control-Allow-Origin "*"
    Header always set Access-Control-Allow-Headers "Authorization"
</IfModule>

finally, activate the header and restart the apache

sudo a2enmod headers

sudo service apache2 restart

Was my answer helpful? You can accept it as a solution.
200+ Premium Magento 2 Extensions Need help? Hire Magento Developer

Re: Custom REST API - Error 400 - Specified request cannot be processed

Sorry @theMageComp should have mentioned, NGINX under Ubuntu 18.04.


@theMageComp wrote:

Hello @drw_ 

Can you try this?

Change the options request to 200 ok

RewriteCond %{REQUEST_METHOD} OPTIONS RewriteRule ^(.*)$ $1 [R=200,L]

<IfModule mod_headers.c>
    Header always set Access-Control-Allow-Origin "*"
    Header always set Access-Control-Allow-Headers "Authorization"
</IfModule>

finally, activate the header and restart the apache

sudo a2enmod headers

sudo service apache2 restart


 

Re: Custom REST API - Error 400 - Specified request cannot be processed

Anyone?

Re: Custom REST API - Error 400 - Specified request cannot be processed

So for posterity the issue was a misunderstanding on the route url in webapi.xml. 

I thought the method in the service element was appended to the url in route element (NFI why i thought that!).

So {base_url}/rest/V1/Company/Product/getProducts should have been {base_url}/rest/V1/Company/Product.