I'm trying to develop a module that creates a custom API endpoint (let's call it `/v1/collector`) that receives any POST request (unauthenticated) and proxies it to a third-party API and then sends back the API's response (headers and body) to the client. I followed this tutorial https://developer.adobe.com/commerce/php/tutorials/backend/create-custom-rest-api/ but so far I couldnt even make my endpoint return a 'Hello World' message. Am I missing something? Is it even possible to do this in Magento?
When I run the setup:upgrade I can see my new module in the list but everytime I try to make a request I get this:
> curl -L 'https://magento:8890/rest/v1/collector' \
-H 'Content-Type: application/json' \
-d '{"test": "test"}'
> {"message":"Specified request cannot be processed.","trace":null} Here are my module's files:
app/code/Dev/RequestForwarder/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="Dev_RequestForwarder">
<sequence>
<module name="Magento_Webapi" />
<module name="Magento_Catalog" />
</sequence>
</module>
</config>
app/code/Dev/RequestForwarder/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="Dev\RequestForwarder\Api\ForwarderInterface"
type="Dev\RequestForwarder\Model\Api\Forwarder"
/>
</config>app/code/Dev/RequestForwarder/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/collector" method="POST">
<service class="Dev\RequestForwarder\Api\ForwarderInterface" method="proxy" />
<resources>
<resource ref="anonymous"/>
</resources>
</route>
</routes>app/code/Dev/RequestForwarder/Api/ForwarderInterface.php
<?php
namespace Dev\RequestForwarder\Api;
use Magento\Framework\Webapi\Rest\Request;
interface ForwarderInterface
{
/**
* Proxies the given data to another service.
*
* @api
*
* @param Request $request The request to proxy.
* @return mixed The response from the proxied service.
*/
public function proxy(Request $request);
}app/code/Dev/RequestForwarder/Model/Api/Forwarder.php
<?php
namespace Dev\RequestForwarder\Model\Api;
use Magento\Framework\Webapi\Rest\Request;
use Dev\RequestForwarder\Api\ForwarderInterface;
class RequestForwarder implements ForwarderInterface
{
/**
* Proxies the given data to another service.
*
* @api
*
* @param Request $request The request to proxy.
* @return mixed The response from the proxied service.
*/
public function proxy(Request $request)
{
// Perform here the logic to forward the request to the other service
// and return the response (headers and body) from that service.
return "Hello World!";
}
}
Just a quick look at your files, you have an error in your last file app/code/Dev/RequestForwarder/Model/Api/Forwarder.php
should be:
class Forwarder implements ForwarderInterface