0

I wrote a custom api for magento 2.3.1

When i call the endpoint that i was created i get the following error

{
"messages": {
    "error": [
        {
            "code": 500,
            "message": "Server internal error. See details in report api/1560842508114"
        }
    ]
}
}

Report detail:

"Fatal Error: 'Uncaught Error: Cannot instantiate interface Demo\Hello\Api\HelloInterface in /var/www/html/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php:116\nStack trace:\n#0 /var/www/html/vendor/magento/framework/ObjectManager/Factory/Compiled.php(108): Magento\Framework\ObjectManager\Factory\AbstractFactory->createObject('Demo\\Hello\\Api\\...', Array)\n#1 /var/www/html/vendor/magento/framework/ObjectManager/ObjectManager.php(70): Magento\Framework\ObjectManager\Factory\Compiled->create('Demo\\Hello\\Api\\...')\n#2 /var/www/html/vendor/magento/module-webapi/Controller/Rest/SynchronousRequestProcessor.php(90): Magento\Framework\ObjectManager\ObjectManager->get('Demo\\Hello\\Api\\...')\n#3 /var/www/html/vendor/magento/module-webapi/Controller/Rest.php(188): Magento\Webapi\Controller\Rest\SynchronousRequestProcessor->process(Object(Magento\Framework\Webapi\Rest\Request\Proxy))\n#4 /var/www/html/vendor/magento/framework/Interception/Interceptor.php(58): Magento\Webapi\Controller\Rest->dispatch(Object(Magento\Fr' in '/var/www/html/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php' on line 116"

app/code/Demo/Hello/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Demo_Hello',
__DIR__
);

app/code/Demo/Hello/Api/HelloInterface.php

<?php
namespace Demo\Hello\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);
}

app/code/Demo/Hello/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="Demo\Hello\Api\HelloInterface"
                type="Demo\Hello\Model\Hello" />
</config>

app/code/Demo/Hello/Model/Hello.php

<?php
namespace Demo\Hello\Model;
use Demo\Hello\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;
   }
}
  • 2
    As I see, your folder structure isn't correct. For example, app/code/Demo/etc/di.xml and app/code/Demo/registration.php – Khoa TruongDinh Dec 18 '19 at 6:18
  • Yeah your Hello class shouldn't be in registration.php, why is it there? Put it in its own file in Demo\Hello\Model\Hello.php – BotMaster Dec 19 '19 at 3:34
0

I would suggest trying to use swagger to execute your API. Also, please refer below links that will help you find out the issue in your current code.

https://inchoo.net/magento-2/magento-2-custom-api/
https://www.scommerce-mage.com/blog/magento-2-how-to-create-custom-api.html

Hope this will help you.

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.