0

I have created a custom rest api module. When i am using GET method it's working fine. But when i am trying to use POST method it giving below error

Request does not match any route.

Does anyone have any idea why it is not working?

My webapi.xml

Second method (GET) working but First method (POST) not working

<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
    <route url="/V1/test/info" method="POST">
        <service class="Bwip\ApiEndpoints\Api\CustomInterface" method="info"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
    </route>
    <route url="/V1/test/data" method="GET">
        <service class="Bwip\ApiEndpoints\Api\CustomInterface" method="data"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
    </route>
</routes>

My di.xml file

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Bwip\ApiEndpoints\Api\CustomInterface" type="Bwip\ApiEndpoints\Model\Api\Custom"/>
</config>

Interface file

interface CustomInterface{

    /**
     * GET for Post api
     * @param string $value
     * @return string
     */

    public function info();
    /**
     * GET for Post api
     * @param string $value
     * @return string
     */

    public function data();
}

My Model

<?php
namespace Bwip\ApiEndpoints\Model\Api;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\App\RequestInterface;
use Bwip\ApiEndpoints\Api\CustomInterface;
 
class Custom implements CustomInterface
{
    /**
     * Returns greeting message to user
     *
     * @api
     * @param string $name User name.
     * @return string Greeting message with users name.
     */
    protected $request;

    public function __construct( 
        RequestInterface $request,
        array $data = []
    ){
        $this->request = $request;
    
    } 
    public function info(){
        $post= $this->request->getPost();
        
        $info[] = array('message' => 'POST Working.', 'status'=> 0);
        return $info;

    }
    public function data(){
        $post= $this->request->getPost();
        
        $info[] = array('message' => 'GET Working.', 'status'=> 0);
        return $info;

    }

}

Thanks in advance.

13
  • How are you trying them, using something like postman? Can you please add the REST Call? Commented Jan 21, 2021 at 17:15
  • @AlanZavagli yes, I am using postman domain.com/rest/V1/test/data (Method GET, works) domain.com/rest/V1/test/info (Method POST, does not works)
    – Nayem323
    Commented Jan 21, 2021 at 17:52
  • Hello have you pass the preference of interfaces in di.xml file
    – Jay Parmar
    Commented Jan 22, 2021 at 4:19
  • can you please add here more file code for rest api i thing 3-4 files are reuired
    – Jay Parmar
    Commented Jan 22, 2021 at 4:23
  • @JayParmar Please see now. I have updated my question details. Thanks
    – Nayem323
    Commented Jan 22, 2021 at 4:32

1 Answer 1

0

Please find solution here

custom.php

public function info(){
        echo "string";

    }

CatalogInterface.php

 /**
     * GET for Post api
     * @param string $value
     * @return string
     */

    public function info();

in postman pass some raw data for post method you used

enter image description here

in post man header you pass like that

enter image description here

and in Authorization please select no-Auth option

enter image description here

2
  • Did everything as same as this still same problem
    – Nayem323
    Commented Jan 22, 2021 at 5:29
  • you r issued in url please pass url like that domain.com/rest/V1/test/info
    – Jay Parmar
    Commented Jan 22, 2021 at 5:38

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.