Связанный вопрос создать пользовательский API в Magento 2 - PullRequest
1 голос
/ 08 мая 2019

Я пытаюсь создать вызов POST для пользовательского API для magento 2, но я не знаю, как это сделать.Как правильно выполнить этот запрос?

мой код:

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"
} }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...