0
<routes>
    <route_collection>
        <route>/cart/addtocart/:device_id/:product_id/:qty</route>
        <action_type>collection</action_type>
     </route_collection>
     <route_entity>
       <route>/cart/addtocart/:device_id/:product_id/:qty</route>
        <action_type>entity</action_type>
     </route_entity>
</routes>

Can we not use same route for rest API call ? <route_collection> with action type collection for POST method and <route_entity> with action type entity for PUT method?

When I tried the above I am getting the below error.

{"messages":{"error":[{"code":405,"message":"Resource method not implemented yet."}]}}

Thanks.!

1 Answer 1

1

I have done following way:

Step 1: creating api2.xml in app\code\local\YourPackageName\Restapi\etc as shown beelow :

<?xml version="1.0" encoding="utf-8"?>
<config>
 <api2>
    <resource_groups>
        <restapi translate="title" module="restapi">
            <title>Restapi</title>
            <sort_order>10</sort_order>
        </restapi>
    </resource_groups>
    <resources>
       <addtocart translate="title" module="restapi">
            <group>restapi</group>
            <model>restapi/api2_addtocart</model>
            <title>Add to Cart</title>
            <sort_order>10</sort_order>
            <privileges>
                <guest>
                    <create>1</create>
                    <retrieve>1</retrieve>
                    <update>1</update>
                    <delete>1</delete>
                </guest>
            </privileges>
            <attributes translate="data" module="restapi">
                <data>Data</data>
                <productid>Product Id</productid>
                <itemid>Item Id</itemid>
                <productname>Product Name</productname>
                <qty>Qty</qty>
                <price>Price</price>
                <options>Options</options>
            </attributes>
            <routes>
                 <route_collection>
                    <route>/addToCart</route>
                    <action_type>collection</action_type>
                </route_collection>
                <route_entity>
                    <route>/addToCart/:device_id/:product_id/:qty</route>
                    <action_type>entity</action_type>
                </route_entity>
            </routes>
            <versions>1</versions>
        </addtocart>
    </resources>
</api2>

Step2: Creating model in app\code\local\YourPackageName\Restapi\Model\Api2\Addtocart.php as shown bellow :

class YourPackageName_Restapi_Model_Api2_Addtocart extends     Mage_Api2_Model_Resource
{
}   

Step3: Finally extending the model addtocart for Guest user in app\code\local\Bluehorse\Restapi\Model\Api2\Addtocart\Rest\Guest\V1.php as shown bellow :

class YourPackageName_Restapi_Model_Api2_Addtocart_Rest_Guest_V1 extends YourPackageName_Restapi_Model_Api2_Addtocart
{
 protected function _create(array $data){
  echo '<pre>';
  print_r($data);
  exit;

  }

 protected function _update(array $data){
  echo '<pre>';
  print_r($data);
  exit;

  }
}   

** POST url:/api/rest/addToCart ** PUT url: /api/rest/addToCart/[device_id]/[product_id]/[qty]

Hope this will help you.

14
  • I have tried this , and for post it is working,When i give Put method it is showing the error
    – Melvin
    Commented Feb 16, 2017 at 13:38
  • is _update method is your model? Commented Feb 16, 2017 at 13:40
  • Sorry .I did not get that ?
    – Melvin
    Commented Feb 16, 2017 at 13:41
  • 1
    You can use same base url but parameter should be different @Melvin Commented Feb 17, 2017 at 5:40
  • 1
    As per as my knowledge it is not provided by magento Commented Feb 17, 2017 at 6:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.