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.

asked Jan 21, 2021 at 16:37
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) Commented Jan 21, 2021 at 17:52
  • Hello have you pass the preference of interfaces in di.xml file 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 Commented Jan 22, 2021 at 4:23
  • @JayParmar Please see now. I have updated my question details. Thanks 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

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.