cancel
Showing results for 
Search instead for 
Did you mean: 

POST REST API Magento 2

POST REST API Magento 2

Hi Guys, I am struggling with POST REST api's in Magento 2 as I am quiet new to it. If its possible for you to provide me one post example like creation of product using oauth and admin account. Or add to cart. I am in bit hurry that's why looking for quick solution. Please please help me. Thanks in Advance.

1 REPLY

Re: POST REST API Magento 2

Hello nidu,
There are a lot of example in Magento2 core module itself, you can check out there.
Here I am briefing basic steps to make a POST REST API in Magento 2.

 

step 1. create a webapi.xml , under etc/ directory
This file contains your routing information and methods which handle your implementation login. Here I am using token based authentication so I have used parameters customerId. You can get capture this id in your function implementation.

 

<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
<route url="/V1/wishlist/add" method="POST">
<service class="Magento\Wishlist\Api\WishlistManagementInterface" method="addWishlistForCustomer"/>
<resources>
<resource ref="self" />
</resources>
<data>
<parameter name="customerId" force="true">%customer_id%</parameter>
</data>
</route>
</routes>

step 2. create a di.xml if not exist in etc/.
This contains your interface and implementation class information. Example

 

 <preference for="\Magento\Wishlist\Api\WishlistManagementInterface" type="\Magento\Wishlist\Model\WishlistManagement" />

step3. create a file WishlistManagementInterface.php, the path must be same as shown in above code. An example of interface

 

<?php
/**
 * Contributor company: iPragmatech solution Pvt Ltd.
 * Contributor Author : Manish Kumar
 * Date: 23/5/16
 * Time: 11:55 AM
 */
namespace Magento\Wishlist\Api;

/**
 * Interface WishlistManagementInterface
 * @api
 */
interface WishlistManagementInterface
{


    /**
     * Return Added wishlist item.
     *
     * @param int $customerId
     * @param int $productId
     * @return array
     *
     */
    public function addWishlistForCustomer($customerId,$productId);
}

step4. Create a file and class with the name WishlistManagement.php, the path must be same as the example of step2.

your implementation function goes here.
/**
* Defines the implementaiton class of the WishlistManagementInterface
*/
class WishlistManagement implements WishlistManagementInterface
{
public function addWishlistForCustomer($customerId, $productId)
{
if ($productId == null) {
throw new LocalizedException(__
('Invalid product, Please select a valid product'));
}
}
}

 Now you url :

http://yourmagento.com/rest/V1/wishlist/add

and post parameter as

{"productId":"6"
}

 

Try once if there is an issue let me know

We are creating wishlist API and going to contribute with Magento2 core, if unfortunately not possible you can check out free module for the same from our site.

 

iPragmatech

Keep in touch with updates.

Thanks.