TransWikia.com

Magento 2 rest api webapi.xml rules for GET parameters

Magento Asked on December 12, 2021

http://localhost:8888/rest/all/V1/api/orders/?from=31.05.2020&to=31.06.2020

My webapi xml file is:

<?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/api/orders/" method="GET">
        <service class="LovatApiApiOrdersRepositoryInterface"
                 method="get"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
    </route>
</routes>

Is it possible to add from and to parameters to this route?

One Answer

Yes it's possible.

Note - to make them required you can keep the "force" attribute true, else false.

<?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/api/orders/" method="GET">
        <service class="LovatApiApiOrdersRepositoryInterface"
                 method="get"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
        <data>
            <parameter name="from" force="true">%from%</parameter>
            <parameter name="to" force="false">%to%</parameter>
        </data>
    </route>
</routes>

And your Interface should be like this

/**
 * @param string $from
 * @param string|null $to
 * @return string
 */
public function get($from, $to = null);

Notice that I've kept $to as null because we have not made it required.

Answered by Afreen Sheikh on December 12, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP