I am trying to use magento2 Rest API method to generate the response for mobile APP.
For that created Following Structure:
Module: Company/Manufacture
Company/Manufacture/composer.json
Company/Manufacture/registration.php - Register Module
Company/Manufacture/etc/module.xml - To Enable Module
Company/Manufacture/API/CustomerInterface.php - Following Code in this file:
namespace Company\Manufacture\Api; interface CustomerInterface { public function customerlist(); }
Company/Manufacture/etc/webapi.xml: Following code:
<?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/mobileapp/customer/customerlist" method="GET"> <service class="Company\Manufacture\Api\CustomerInterface" method="customerlist"/> <resources> <resource ref="anonymous"/> </resources> </route> </routes>
Company/Manufacture/Model/Customer.php: Following code:
namespace Company\Manufacture\Model; use Company\Manufacture\Api\CustomerInterface as ApiInterface; class Customer implements ApiInterface { public function customerlist(){ $data = array("Dance","Success"); $test = array(['success'=>true,'data'=>$data); return $test; } }
Now after doing all these settings when running following url throws 404 error:
http://127.0.0.1/M224/V1/mobileapp/customer/customerlist
Not sure why getting 404 Error. Am i missing any settings? Do i need to configure anything on admin for this?
Is there any settings i need to do on admin to run REST API?
Any help would be appreciated.
You should do next steps form make it work.
1) Add DI config with service definition
Example:
<preference for="Acme\RestApi\Api\HelloInterface" type="Acme\RestApi\Model\HelloService" />
2) Fix ACL definition
From:
<resource ref="anonymous"/>
To:
<resources> <resource ref="anonymous" /> </resources>
3) Add DOC block for interface method (It is required for Reflection class)
Example:
interface HelloInterface
{
/**
* Get Hello message
*
* @return string
*/
public function sayHello();
}
4) Change your URL by this pattern:
{sheme}://{base}/rest/{endpoint}
In your case if your base url `http://127.0.0.1/M224/` it must be something like this:
http://127.0.0.1/M224/rest/V1/mobileapp/customer/customerlist
Here you can check full working example: link (30days only)