Magento 2 Api获取错误'%fieldName是必填字段'?


9

使用url的POST方法时我未得到结果(id),即rest / V1 / hello / test / 3

屏幕截图在此处输入图片说明

我已点击此链接以供参考,请单击此处

1)webapi.xml

<?xml version="1.0"?>
    <routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
        <route url="/V1/hello/name/:name" method="GET">
            <service class="Inchoo\Hello\Api\HelloInterface" method="name"/>
            <resources>
                <resource ref="anonymous"/>
            </resources>
        </route>
        <route url="/V1/hello/test/:test" method="POST">
            <service class="Inchoo\Hello\Api\TestInterface" method="test"/>
            <resources>
                <resource ref="anonymous"/>
            </resources>
        </route>
    </routes>

2)TestInterface.php

<?php
namespace Inchoo\Hello\Api;

interface TestInterface
{
    /**
     * Returns greeting message to user
     *
     * @api
     * @param id $name Users id.
     * @return id Greeting message with users id.
     */
    public function test($id);
}

3)Test.php

<?php
namespace Inchoo\Hello\Model;
use Inchoo\Hello\Api\TestInterface;

class Test implements TestInterface
{
    /**
     * Returns greeting message to user
     *
     * @api
     * @param string $name Users name.
     * @return string Greeting message with users name.
     */
    public function test($id) {
        return "Hello How are you your id is:," .$id;
    }
}

4)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="Inchoo\Hello\Api\HelloInterface" type="Inchoo\Hello\Model\Hello" />
    <preference for="Inchoo\Hello\Api\TestInterface" type="Inchoo\Hello\Model\Test" />
</config>

现在,我清除了缓存和页面缓存,并打开了邮递员应用程序,并保留了网址,例如http://10.0.0.33/nagarajuM2/rest/V1/hello/test/3

但是我遇到了错误。

请帮帮我。


您正在发送POST请求,并且正在发送什么数据?
Emipro Technologies Pvt。公司

实际上我想使用post方法打印id值,因此我使用了inchoo遵循的相同概念。而在网址中,我使用的像是rest / V1 / hello / name / 4
Nagaraju K

1
要进行打印,首先必须发送POST数据,例如json:{“ id”:{}}
Emipro Technologies Pvt。公司

好的,让我尝试..
Nagaraju K

感谢@Emipro技术,现在我得到了结果。谢谢你的帮助。 prntscr.com/g0d53x
Nagaraju K

Answers:



6

为接口文件和实现它的类正确更新函数注释。

interface TestInterface
{
    /**
     * Returns greeting message to user
     *
     * @param int $name
     * @return mixed
     */
    public function test($id);
}

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.