cancel
Showing results for 
Search instead for 
Did you mean: 

Debugging custom REST APIs

Debugging custom REST APIs

Hi,

 

I am trying to write an example module with a REST service 'Calculator' as described in here

The module shows up on "php bin/magento module:status" list as 'enabled'.

 

However, when I try to access the server via the endpoint

http://127.0.0.1:8080/magento2/rest/V1/calculator/1/2

I just get the following error:

Request does not match any route.

I tried the built in REST APIs and they seem to work. Hence the problem is with this custom module.

Unfortunately I do not see any errors in the logs. 

 

Now, my question is - how do you approach such problems? 

What is the main controller class handling REST endpoints that could potentially give some clues?

 

I use Magento 2.1.

 

Thanks,

Gin

11 REPLIES 11

Re: Debugging custom REST APIs

Hello,

Re-compile magento and clear the cache if does not work then can you please paste your code for the following:

1. webapi.xml

2. di.xml

3. interface.php

4. and model where you implemented the interface.

 

Re: Debugging custom REST APIs

Hi Manish,

 

Thanks for the reply!

Tried recompiling - no luck.

Please see the source code/configuration below:

 

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">
    <!-- Example: curl http://localhost:8080/magento2/rest/V1/calculator/add/1/2 -->
    <route url="/V1/calculator/add/:num1/:num2" method="GET">
        <service class="Test\CalculatorWebService\Api\CalculatorInterface" method="add"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
    </route>
</routes>

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="Test\CalculatorWebService\Api\CalculatorInterface"
                type="Test\CalculatorWebService\Model\Calculator" />
</config>

CalculatorInterface.php

<?php
namespace Test\CalculatorWebService\Api;

interface CalculatorInterface
{
    /**
     * Return the sum of the two numbers.
     *
     * @api
     * @param int $num1 Left hand operand.
     * @param int $num2 Right hand operand.
     * @return int The sum of the numbers.
     */
    public function add($num1, $num2);
}

Calculator.php

<?php

namespace Test\CalculatorWebService\Model;

use Test\CalculatorWebService\Api\CalculatorInterface;

class Calculator implements CalculatorInterface
{
    /**
     * Return the sum of the two numbers.
     *
     * @api
     * @param int $num1 Left hand operand.
     * @param int $num2 Right hand operand.
     * @return int The sum of the two values.
     */
    public function add($num1, $num2) {
        return $num1 + $num2;
    }
}

Thanks,

Gin

 

Re: Debugging custom REST APIs

Ok, that's embarrassing - the endpoint supposed to be:

.../rest/V1/calculator/add/1/2

Closing the thread.

Re: Debugging custom REST APIs

Hello,

Sorry for the late response. Glad to see that you fixed you issue. If you have any other issue regarding Magento2 REST API let us know.

Re: Debugging custom REST APIs

I am not able whatever the fixes you have done here. Can you explain here. which will helpful for me to understand. 

Re: Debugging custom REST APIs

Hello ,

I integrate custom module for API in magento V 2.0.2 , It is activated. But when i run my URL : http://localhost/magento2/rest/V1/order/myorder it gives me :

<response><message>Request does not match any route.</message></response>

This error . Please help me i am new to Magento 2 .

Guide me the fruitful suggestion to resolve my issue.

 

Thank you

Re: Debugging custom REST APIs

Hi,

I think you have not installed the module properly. Please reinstall it if
the error exists please send us the server credential for that.

skype: manish.ip7

Re: Debugging custom REST APIs

Hello sir,

Thank you for your reply. I refer following links for creating module :

1) https://www.ipragmatech.com/extend-magento2-rest-api-easy-steps/

2) https://www.demacmedia.com/extending-magento-2-rest-web-api/

I created module as per the steps given in above link.

 

Please give me clarification of folder and file path for module creation. Because i re-integrate module as per your suggestion but still same error i got. I am trying this at my local host.

 

Please tell me file/folder path for creation of custom REST API module.

eagerly waiting for your suggestion.

Thank you.

Re: Debugging custom REST APIs

Hello sir,

 

I want to get all root categories of my store using Rest API , for that i use this URL : http://magento2.localhost/index.php/rest/all/V1/categories

 

But i got this error when i hit this URL.

{"message":"Cannot perform GET operation with store code 'all'"}

 

Please suggest me solution for getting all root categories in magento2.

 

Thank You.