문제

I am not getting result (id) while using POST method using url is rest/V1/hello/test/3

Screenshot: enter image description here

I have followed this link For Reference Please Click Here

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>

Now i cleared cache and pagecache and opened the postmen app and kept the url like http://10.0.0.33/nagarajuM2/rest/V1/hello/test/3

but i am getting error.

Please help me out.

도움이 되었습니까?

해결책

You are Sending POST Request so you also have to send data in POST request in json format like :{ "id": {}}

And have to set Content-Type:application/json in http header.

다른 팁

Update the function comments correctly for both Interface file and class that implement it.

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

I was receiving the same issues, then I noticed that I was using POST instead of GET, changing it the issues were solved.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 magento.stackexchange
scroll top