我正在尝试创建对magento 2的自定义API的POST调用,但是我不知道该怎么做。发出此请求的正确方法是什么?
我的代码:
webapi.xml
<?xml version="1.0"?>
<routes xmlns:xsi="http://www.ws3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
<route url="/V1/rest/produtos/" method="POST">
<service class="Api\Rest\Api\HelloInterface" method="name"/>
<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="Api\Rest\Api\HelloInterface" type="Api\Rest\Model\Hello" />
</config>
HelloInterface.php
namespace Api\Rest\Api;
interface HelloInterface
{
public function name($nome);
}
Hello.php
namespace Api\Rest\Model;
use Api\Rest\Api\HelloInterface;
class Hello implements HelloInterface
{
protected $request;
public function __construct(
\Magento\Framework\App\RequestInterface $request
)
{
$this->_request = $request;
}
public function name($nome)
{
//$params = $this->_request->getParams();
return "test";
}
}
我的要求:
标题:
Content-Type: application/json
发送:
{"nome":"test"}
返回正文:
{
"message": "\"%fieldName\" is required. Enter and try again.",
"parameters": {
"fieldName": "nome"
} }