0

I use a custom API with Delete Method. I would get this paramter.

{
    "id":"1"
}

How can i get it in my api response?

etc/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/news/news/delete" method="DELETE">
        <service class="[Vendor]\[Module]\Api\NewsInterface" method="destroy"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
    </route>
</routes>

Api/NewsInterface.php

<?php
namespace [vendor]\[module]\Api;

/**
* @api
*/

interface NewsInterface
{

    /**
     * Return Added wishlist item.
     *
     * @param int $customerId
     * @return int
     *
     */

    public function destroy($customerId);

}

Model/NewsManagement.php

<?php
namespace [Vendor]\[Module]\Model;
use [Vendor]\[Module]\\Api\NewsInterface;

class NewsManagement implements NewsInterface
{
    protected $newsFactory;
    protected $request;

    public function __construct(
        \Magento\Framework\Webapi\Rest\Request $request,
        \[Vendor]\[Module]\\Model\NewsFactory $newsFactory
    )
    {
        $this->newsFactory = $newsFactory;
        $this->request = $request;
    }
    public function destroy($customerId) {
        return $customerId;
    }

}

2 Answers 2

1

You need to define the parameter that will be sent by Magento to the service contract from the webapi.xml: /:customerId

Your webapi.xml file will look like this:

<?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/news/news/delete/:customerId" method="DELETE">
        <service class="[Vendor]\[Module]\Api\NewsInterface" method="destroy"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
    </route>
</routes>

And then you can make a DELETE request to /V1/news/news/delete/1 to remove the item associated with customerId 1.

Here is how Magento has implemented the DELETE customer:

4
  • yes i know this solutions but what if i would update a item with params? Commented Oct 7, 2019 at 6:25
  • what exactly do you mean by "update an item with parameters" ?
    – bluscience
    Commented Oct 7, 2019 at 11:02
  • its just a put method Commented Oct 7, 2019 at 11:18
  • you can apply the same method, but instead of the DELETE method, you need to add PUT. <route url="/V1/news/news/:customerId" method="PUT">
    – bluscience
    Commented Oct 7, 2019 at 19:08
0

you have change parameters name $customerId to $id at both interface and model class.

<?php
namespace [vendor]\[module]\Api;

/**
* @api
*/

interface NewsInterface
{

    /**
     * Return Added wishlist item.
     *
     * @param int $id
     * @return int
     *
     */

    public function destroy($id);

}

and Model/NewsManagement.php


<?php
namespace [Vendor]\[Module]\Model;
use [Vendor]\[Module]\\Api\NewsInterface;

class NewsManagement implements NewsInterface
{
    protected $newsFactory;
    protected $request;

    public function __construct(
        \Magento\Framework\Webapi\Rest\Request $request,
        \[Vendor]\[Module]\\Model\NewsFactory $newsFactory
    )
    {
        $this->newsFactory = $newsFactory;
        $this->request = $request;
    }
     /**
     * @inheritdoc
     *
     */
    public function destroy($id) {
        return $id;
    }

}

1
  • its said \"%fieldName\" is required. Enter and try again.","parameters":{"fieldName":"id"},"trace":"#0 Commented Oct 6, 2019 at 14:51

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.