I'm trying to create an api route with input parameters but I'm not getting the expected result.
Here is my webapi.xml:
<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../app/code/Magento/Webapi/etc/webapi.xsd">
<route url="/V1/foo" method="POST" secure="true">
<service class="..\FooInterface"
method="getFooById"/>
<resources>
<resource ref="anonymous"/>
</resources>
<data>
<parameter name="id" force="true">%id%</parameter>
</data>
</route>
</routes>
Here my interface:
interface FooInterface
{
/**
* Test function
*
* @api
* @param string $id
* @return string
*/
public function getFooById($id);
}
And here the class:
class Foo implements FooInterface
{
/**
* {@inheritdoc}
*/
public function getFooById($id){
return $id;
}
}
I'm omitting the di.xml with the preference.
Now if I call that route whatever I do all I get back is %id%
. Even if I don't put any parameter I get that instead of an error or something.
What am I doing wrong here?
I tried:
- sending a get request
- sending a get request with parameter
/order?id=foo
- sending a post request
- sending a post request with form-data
- sending a post request with raw data (as seen in screenshot)
All have the same result -> %id%
I'm using community edition btw.
Best Answer
After check the core modules, found that
parameter
node in webapi.xml is only used in Customer Module forself
resource type.Below file is responsible for Convert parameter placeholder with value.
Now there are two ways to pass parameter in API.
Solution 1:
webapi.xml
like below:and specify
$id
argument in Interface class and in implementation definition also. In this case, you need to pass id as body content.Solution 2:
webapi.xml
like below:and specify
$id
argument in Interface class and in implementation definition also. In this case, you need to pass id in URL as argument. For this you can check product module also.