Skip to content

Request does not match any route error Magento 2 API

I’ve created an extension , while visiting

magento.com/rest/V1/wishlist/1/2

it throws error.

here are my files –

di.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="YourCompanyWishlistWebServiceApiWishlistManagementInterface" type="YourCompanyWishlistWebServiceModelWishlistManagement" />
</config>

webapi.xml

<?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/wishlist/:customerId" method="GET">
        <service class="YourCompanyWishlistWebServiceApiWishlistManagementInterface" method="getWishlistForCustomer"/>
        <resources>
            <resource ref="anonymous" />
        </resources>
    </route>
    <route url="/V1/wishlist/:customerId/:productId" method="POST">
        <service class="YourCompanyWishlistWebServiceApiWishlistManagementInterface" method="addWishlistForCustomer"/>
        <resources>
            <resource ref="anonymous" />
        </resources>
    </route>    
    <route url="/V1/wishlist/:customerId/:productId" method="DELETE">
        <service class="YourCompanyWishlistWebServiceApiWishlistManagementInterface" method="addWishlistForCustomer"/>
        <resources>
            <resource ref="anonymous" />
        </resources>
    </route>    
</routes>

Api/WishlistManagementInterface.php

<?php
/**
 * A Magento 2 module named YourCompany/WishlistWebService
 *  
 */
namespace YourCompanyWishlistWebServiceApi;

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

    /**
     * Return Wishlist items.
     *
     * @param int $customerId
     * @return string
     */
    public function getWishlistForCustomer($customerId);

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

}

YourCompanyWishlistWebServiceModelWishlistManagement.php

<?php
/**
 * A Magento 2 module named YourCompany/WishlistWebService
 *  
 */
namespace YourCompanyWishlistWebServiceModel;

use YourCompanyWishlistWebServiceApiWishlistManagementInterface;
use MagentoWishlistControllerWishlistProvider;
use MagentoWishlistModelResourceModelItemCollectionFactory;
use MagentoWishlistModelWishlistFactory;
use MagentoCatalogApiProductRepositoryInterface;
use MagentoFrameworkExceptionLocalizedException;
use MagentoCatalogHelperImageFactory as ProductImageHelper;
use MagentoStoreModelAppEmulation as AppEmulation;
/**
 * Defines the implementaiton class of the WishlistManagementInterface
 */
class WishlistManagement implements WishlistManagementInterface
{

    /**
     * @var CollectionFactory
     */
    protected $_wishlistCollectionFactory;

    /**
     * Wishlist item collection
     *
     * @var MagentoWishlistModelResourceModelItemCollection
     */
    protected $_itemCollection;

    /**
     * @var WishlistRepository
     */
    protected $_wishlistRepository;

    /**
     * @var ProductRepository
     */
    protected $_productRepository;

    /**
     * @var WishlistFactory
     */
    protected $_wishlistFactory;

    /**
     * @var Item
     */
    protected $_itemFactory;

    /**
     * @var MagentoCustomerModelCustomer
     */
    protected $_customer;

    /**
     *@var MagentoCatalogHelperImageFactory
     */
    protected $productImageHelper;

    /**
     *
     * @var MagentoStoreModelStoreManagerInterface
     */
    protected $storemanagerinterface;

    /**
     *@var MagentoStoreModelAppEmulation
     */
    protected $appEmulation;

    /**
     *@var MagentoCatalogModelProduct
     */
    protected $_productload;

    /**
     *@var MagentoDirectoryModelCountryFactory
     */
    protected $countryfactory;

    /**
     * @param CollectionFactory $wishlistCollectionFactory
     * @param MagentoCatalogModelProductFactory $productFactory
     * @param MagentoFrameworkMathRandom $mathRandom
     * @param MagentoCustomerModelCustomer $customer
     * @param MagentoFrameworkStdlibDateTime $dateTime
     * @param ProductRepositoryInterface $productRepository
     */
    public function __construct(
        CollectionFactory $wishlistCollectionFactory,
        WishlistFactory $wishlistFactory,
        MagentoCustomerModelCustomer $customer,
        AppEmulation $appEmulation,
        MagentoDirectoryModelCountryFactory $countryfactory,
        MagentoStoreModelStoreManagerInterface $storemanagerinterface,
        ProductImageHelper $productImageHelper,
        MagentoCatalogModelProduct $productload,
        MagentoWishlistModelWishlistFactory $wishlistRepository,
        MagentoCatalogApiProductRepositoryInterface $productRepository,
        MagentoWishlistModelItemFactory $itemFactory
    ) {
        $this->_wishlistCollectionFactory = $wishlistCollectionFactory;
        $this->_wishlistRepository = $wishlistRepository;
        $this->_productRepository = $productRepository;
        $this->_wishlistFactory = $wishlistFactory;
        $this->countryfactory = $countryfactory;
        $this->storemanagerinterface = $storemanagerinterface;
        $this->_itemFactory = $itemFactory;
        $this->_customer = $customer;
        $this->_productload = $productload;
        $this->appEmulation = $appEmulation;
        $this->productImageHelper = $productImageHelper;
        $this->_customer = $customer;
    }

    /**
     * Get wishlist collection
     * @deprecated
     * @param $customerId
     * @return string
     */
    public function getWishlistForCustomer($customerId)
    {

        if (empty($customerId) || !isset($customerId) || $customerId == "") {
            throw new InputException(__('Id required'));
        } else {
            $collection =
                $this->_wishlistCollectionFactory->create()
                    ->addCustomerIdFilter($customerId);
            $baseurl = $this->storemanagerinterface->getStore()->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_MEDIA).'catalog/product';
            $wishlistData = [];
            foreach ($collection as $item) {
                $productInfo = $item->getProduct()->toArray();
                if ($productInfo['small_image'] == 'no_selection') {
                  $currentproduct = $this->_productload->load($productInfo['entity_id']);
                  $imageURL = $this->getImageUrl($currentproduct, 'product_base_image');
                  $productInfo['small_image'] = $imageURL;
                  $productInfo['thumbnail'] = $imageURL;
                }else{
                  $imageURL = $baseurl.$productInfo['small_image'];
                  $productInfo['small_image'] = $imageURL;
                  $productInfo['thumbnail'] = $imageURL;
                }
                $data = [
                    "wishlist_item_id" => $item->getWishlistItemId(),
                    "wishlist_id"      => $item->getWishlistId(),
                    "product_id"       => $item->getProductId(),
                    "store_id"         => $item->getStoreId(),
                    "added_at"         => $item->getAddedAt(),
                    "description"      => $item->getDescription(),
                    "qty"              => round($item->getQty()),
                    "product"          => $productInfo
                ];
                $wishlistData[] = $data;
            }
            return $wishlistData;
        }
    }

    /**
     * Add wishlist item for the customer
     * @param int $customerId
     * @param int $productId
     * @return bool
     * @throws MagentoFrameworkExceptionLocalizedException
     */
    public function addWishlistForCustomer($customerId, $productId)
    {
        if ($productId == null) {
            throw new LocalizedException(__
            ('Invalid product, Please select a valid product'));
        }
        try {
            $product = $this->_productRepository->getById($productId);
        } catch (NoSuchEntityException $e) {
            $product = null;
        }
        try {
            $wishlist = $this->_wishlistRepository->create()->loadByCustomerId
            ($customerId, true);
            $wishlist->addNewItem($product);
            $returnData = $wishlist->save();
        } catch (NoSuchEntityException $e) {

        }
        return true;
    }    

    /**
     * Helper function that provides full cache image url
     * @param MagentoCatalogModelProduct
     * @return string
     */
    public function getImageUrl($product, string $imageType = ''){
        $storeId = $this->storemanagerinterface->getStore()->getId();
        $this->appEmulation->startEnvironmentEmulation($storeId, MagentoFrameworkAppArea::AREA_FRONTEND, true);
        $imageUrl = $this->productImageHelper->create()->init($product, $imageType)->getUrl();
        $this->appEmulation->stopEnvironmentEmulation();

        return $imageUrl;
    }
}

Please help me solve this.
Thanks