Custom Api Holidays in Magento

I need a rest api to create a client in magneto so that I follow this tutorial http://www.authenticdesign.co.uk/extending-magento-rest-api-v2/

I followed it step by step, but when I test the api on the rest client, it gives me: {"messages":{"error":[{"code":404,"message":"Request does not match any route."}]}}

I do not know where I am wrong. Help me here as I am very new to magento as well as to php.

Steps:

1. The extension is included in (application / etc / module / Custom_Restapi.xml)

 <config> <modules> <Custom_Restapi> <active>true</active> <codePool>local</codePool> </Custom_Restapi_Groups> </modules> </config> 

2.config.xml at (app / code / local / Custom / Restapi / etc / config.xml)

  <?xml version="1.0"?> <config> <modules> <Custom_Restapi> <version>0.1.0.0</version> </Custom_Restapi> </modules> <global> <models> <restapi> <class>Custom_Restapi_Model</class> </restapi> </models> </global> </config> 

3.api2.xml at (app / code / local / Custom / Restapi / etc / api2.xml)

 <?xml version="1.0"?> <config> <api2> <resource_groups> <restapi translate="title" module="Custom_Restapi"> <title>Custom Rest API</title> <sort_order>10</sort_order> </restapi> </resource_groups> <resources> <restapi translate="title" module="Custom_Restapi"> <group>restapi</group> <model>restapi/api2_restapi</model> <title>Testing My Rest API</title> <sort_order>10</sort_order> <privileges> <admin> <create>1</create> </admin> </privileges> <attributes translate="" module="Custom_Restapi"> <firstname>First Name</firstname> <lastname>Last Name</lastname> <email>Email</email> <password>Password</password> </attributes> <routes> <route> <route>/customer</route> <action_type>collection</action_type> </route> </routes> <versions>1</versions> </restapi> </resources> </api2> </config> 

4. Model Class Restapi.php at (app / code / local / Custom / Restapi / Model / Api2 / Restapi.php)

 <?php class Custom_Restapi_Model_Api2_Restapi extends Mage_Api2_Model_Resource { } ?> 

5. V1.php at (app / code / local / Custom / Restapi / Model / Api2 / Restapi / Rest / Admin / V1.php)

 <?php class Custom_Restapi_Model_Api2_Restapi_Rest_Admin_V1 extends Custom_Restapi_Model_Api2_Restapi { /** * Create a customer * @return array */ public function _create() { $requestData = $this->getRequest()->getBodyParams(); $firstName = $requestData['firstname']; $lastName = $requestData['lastname']; $email = $requestData['email']; $password = $requestData['password']; $customer = Mage::getModel("customer/customer"); $customer->setFirstname($firstName); $customer->setLastname($lastName); $customer->setEmail($email); $customer->setPasswordHash(md5($password)); $customer->save(); return json_encode(array("testing","Success")); } } ?> 

And my url is like: baseurl / api / rest / customer

+6
source share
2 answers

I would put this in a comment, because I believe that this is not a complete answer, but I have not yet been allowed. A few things:

  • Your global tag in config.xml is not closed.

  • You cannot create entries using a URL that refers to objects, you must use the collection route defined in route_collection node in api2.xml. Therefore you should call / api / rest / customer.

  • There is no need to have a separate β€œcreate” route, since the method is selected by the http (post / get / delete / etc) method and the body content. I would recommend the route "customer /: id" for the route_entity element. Also make sure you send the HTTP POST.

I could not reproduce the exact error you posted, but I was able to get this work after correcting the above points.

Also, be sure to grant permission to this resource in the administration area and clear the web service configuration caches.

The specific exception you specified is selected in Mage_Api2_Model_Router in the route method.

I reworked this and created a repo on github with a working module: https://github.com/themizzi/Custom-Magento-Rest-Api2 . The module uses guest access, since I did not have time to go through the whole oAuth deal, but if you just upgrade the guest node in api2.xml to the administrator and update your access in the admin area, it will work.

+9
source
  • Firstly, you made a small mistake in

    Step 1. The extension is enabled in (application / etc / module / Custom_Restapi.xml)

    You opened the tag as <Custom_Restapi> , but closed the tag as <Custom_Restapi_Grops> , and you also missed the <?xml version="1.0"?> Tag.

  • Secondly, you can put your code in _retrieveCollection() , as in api2.xml , you have defined only one route and this is to get the collection.

    Put your code in _retrieveCollection() or in _retrieveCollection() call your _create method.

  • Finally, you defined firstname , lastname , email, and amp; password as an attribute in api2.xml . They are not POST parameters, and I'm either not familier with the functioning getBodyParams () method

    Either you must define the routes to get all 4 parameters by the URL in api2.xml or you can try $ _GET [] by adding all your parameters to the query string.

HopeFully it will help you.

thanks

0
source

Source: https://habr.com/ru/post/986742/


All Articles