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.
-
How are you trying them, using something like postman? Can you please add the REST Call?Alan Zavagli– Alan Zavagli2021年01月21日 17:15:07 +00:00Commented 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– Nayem3232021年01月21日 17:52:41 +00:00Commented Jan 21, 2021 at 17:52
-
Hello have you pass the preference of interfaces in di.xml fileJay Parmar– Jay Parmar2021年01月22日 04:19:54 +00:00Commented Jan 22, 2021 at 4:19
-
can you please add here more file code for rest api i thing 3-4 files are reuiredJay Parmar– Jay Parmar2021年01月22日 04:23:03 +00:00Commented Jan 22, 2021 at 4:23
-
@JayParmar Please see now. I have updated my question details. ThanksNayem323– Nayem3232021年01月22日 04:32:05 +00:00Commented Jan 22, 2021 at 4:32
1 Answer 1
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
in post man header you pass like that
and in Authorization please select no-Auth option
-
Did everything as same as this still same problemNayem323– Nayem3232021年01月22日 05:29:39 +00:00Commented Jan 22, 2021 at 5:29
-
you r issued in url please pass url like that domain.com/rest/V1/test/infoJay Parmar– Jay Parmar2021年01月22日 05:38:31 +00:00Commented Jan 22, 2021 at 5:38
Explore related questions
See similar questions with these tags.