12

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.

6
  • You are Sending POST request and in that what data you are sending ? Jul 26, 2017 at 4:51
  • Actually i want to print id value using post method hence i have used the same concept what ever inchoo has followed. and in the url i am using like rest/V1/hello/name/4 Jul 26, 2017 at 4:55
  • 1
    For print first you have to send in POST data like json:{ "id": {}} Jul 26, 2017 at 4:57
  • ok let me try.. Jul 26, 2017 at 4:59
  • Thanks @Emipro technologies now i got the result. thanks for u help. prntscr.com/g0d53x Jul 26, 2017 at 5:12

3 Answers 3

8

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.

1
6

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);
}
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.