Custom Rest Api in Magento
Asked Answered
H

2

6

I need rest api to create customer in magneto for that I followed 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 rest client it give me: {"messages":{"error":[{"code":404,"message":"Request does not match any route."}]}}

I do not have any idea where I am making mistake. Help me out here as I am very new to magento as well as for php.

The steps are:

1. Enabled Extension at (app/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

Hinayana answered 7/5, 2015 at 13:16 Comment(1)
Do you have any about Rest API Error, If i try 127.0.0.1/anusthana/api/rest/customers i getting Failed to load resource: the server responded with a status of 403 (Forbidden). and my products api working good : products api 127.0.0.1/anusthana/api/rest/products, customer api error. Can i get help?Dexterous
W
9

I would put this in a comment since I feel this is not a fully complete answer, but I am not yet allowed to. A few things:

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

  2. You cannot create records using a url that references entities, you have to use the collection route defined in the route_collection node in api2.xml. So you should be calling /api/rest/customer.

  3. There is no need to have a separate "create" route since the method is chosen by the http method (post/get/delete/etc) and the body content. I would recommend a route of "customer/:id" for the route_entity element. So also be sure that you are submitting an HTTP POST.

I was not able to reproduce the exact error you posted, but I was able to get this working after correcting the above items.

Also, be sure to give permission on this resource in the admin area and to clear your Web Services config caches.

The specific exception you listed is thrown in Mage_Api2_Model_Router in the route method.

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

Wideangle answered 7/5, 2015 at 17:6 Comment(9)
sorry for global tag I corrected yesterday itself but not updated here But I am not getting your 2nd point completely can you elabrote little bit more( yes I am using url like this www.myweb.com/api/rest/customer in post request) ThanksHinayana
so you are no longer using this url? And my url is like : apiurl/customer/createWideangle
For the second point, I suggest looking through the Mage_Api2_Model_Router class to see how it actually chooses the method to call in your own model class. It is based on a mix of the HTTP method and the type of body data. That is also where your specific exception is thrown.Wideangle
Ah, another problem is the path to your V1 class. This class should have a path of app/code/local/Custom/Restapi/Model/Api2/Restapi/Rest/Admin/V1.php and the class name should be updated accordingly to Custom_Restapi_Model_Api2_Restapi_Rest_Admin_V1.Wideangle
yes Joe I am using only apiurl/customer only with post.Hinayana
joe can you share your code I am stuck...specifically I am confused with api2 (routes,groups and all). I gave many updates but it is not workingHinayana
@Spartan, I have updated the answer with a link to a github repo with a working version of this module. Please note my comments above.Wideangle
Joe it's working error is in translate of attributes. Thanks :)Hinayana
Hi, can you please describe step by step how to use your code? I followed this tutorial authenticdesign.co.uk/extending-magento-rest-api-v2 and also install your code but it's always 404 not found. ThanksGammy
A
0
  • Firstly you have done small mistake in

    Step 1. Enabled Extension at (app/etc/module/Custom_Restapi.xml)

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

  • Secondly You can put your code in _retrieveCollection() as in api2.xml you have only defined 1 route & that to retrieve collection.

    Either put your code in _retrieveCollection() or in _retrieveCollection() call your _create method.

  • Lastly you have defined firstname, lastname, email & password as attribute in api2.xml They are not POST Parameters, & I am either not familier with functioning of getBodyParams() Method

    Either You have to define routes to get all 4 parameters via URL in api2.xml or You can try $_GET[] by attaching all your parameters in Query String.

HopeFully it will Help You.

Thanks

Ayo answered 24/4, 2016 at 13:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.