2

As I have created a New sample REST Api Call to method GET please look below details

etc/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="Magestore\Magento\Api\HelloInterface" method="name"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
    </route>
</routes>

etc/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="Magestore\Magento\Api\HelloInterface"
                type="Magestore\Magento\Model\Hello" />
</config>

Api/HelloInterface

<?php
namespace Magestore\Magento\Api;

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

Model/Hello.php

<?php
namespace Magestore\Magento\Model;
use Magestore\Magento\Api\HelloInterface;

class Hello implements HelloInterface
{
    /**
     * Returns greeting message to user
     *
     * @api
     * @param string $name Users name.
     * @return string Greeting message with users name.
     */
    public function name($name) {
        return "Hello, " . $name;
    }
}

As you can see the below image giving me the consumer is not authorized to access the resource as, the resource type is anonmoys.

enter image description here

Please can anyone help me to solve these,or else give me idea to resolve this. Advance thanks.

1

You can try it:

etc/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" method="GET">
    <service class="Magestore\Magento\Api\HelloInterface" method="name"/>
    <resources>
        <resource ref="anonymous"/>
    </resources>
   </route>
</routes>

then use api url like:

www.example.com/rest/V1/hello/name/?name=test

Hope this help you.

  • This is not working. – Learing_Coder Sep 13 '18 at 16:44
  • Have you done setup:di:compile also header will be only Content-Type:application/json – Prasanta Hatui Sep 13 '18 at 17:31
  • yes, i have run compile cmd and header as you said content-Type:application/json. – Learing_Coder Sep 13 '18 at 18:11
  • but no change same error. – Learing_Coder Sep 13 '18 at 18:11
  • Looks all fine, only doubts is module name "Magento" . I am not sure but may be this cause the issue. you can try by changing the vendor name. – Prasanta Hatui Sep 13 '18 at 18:40
0

Remove Auth from header it is not needed.

Please flush cache using below command

php bin/magento cache:flush

It is working for me please check below

enter image description here

  • Getting the same error.can you please give more information. – Learing_Coder Sep 13 '18 at 17:12
  • Have you registered your module? please verify your module is active? – Prashant Valanda Sep 15 '18 at 6:16

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.