4

I have created a rest web api. I want to pass multiple parameters to get my required data. How can I do that ?

    interface ProductsInterface
  {
    /**
   * Returns greeting message to user
   *
   * @api
 * @param string $sku
 * @return string Greeting message with users name.
 */
public function getProductPrice($sku);
/**
 * @param $sku
 * @param $attributecode
 * @return mixed
 */
public function getProductAttribute($sku, $attributecode);
}

Model:

class Products implements ProductsInterface
{
/**
 * @var ProductRepositoryInterface
 */
private $_productRepository;
/**
 * Product constructor.
 * @param ProductRepositoryInterface $productRepository
 */
public function __construct(ProductRepositoryInterface $productRepository)
{
    $this->_productRepository = $productRepository;
}
/**
 * Returns greeting message to user
 *
 * @param string $sku
 * @return string Greeting message with users name.
 * @throws \Magento\Framework\Exception\NoSuchEntityException
 * @api
 */
public function getProductPrice($sku)
{
    $prodcutBySku = $this->_productRepository->get($sku);
    return $prodcutBySku->getPrice();
}
/**
 * @param $sku
 * @param $attributecode
 * @return \Magento\Framework\Api\AttributeInterface|null
 * @throws \Magento\Framework\Exception\NoSuchEntityException
 */
public function getProductAttribute($sku, $attributecode)
{
    $prodcutBySku = $this->_productRepository->get($sku);
    return $prodcutBySku->getCustomAttribute($attributecode);
}
}

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/products/getProductPrice/:sku" method="GET">
    <service class="Evamp\Webapi\Api\ProductsInterface" method="getProductPrice"/>
    <resources>
        <resource ref="anonymous"/>
    </resources>
</route>
<route url="/V1/products/getProductAttribute/:sku,:attr" method="GET">
    <service class="Evamp\Webapi\Api\ProductsInterface" method="getProductPrice"/>
    <resources>
        <resource ref="anonymous"/>
    </resources>
</route>

2 Answers 2

1

If you want pass multiple parameter into api . Define parameter type function above comment section '@param array $data'. It's mean parameter is array(multiple parameter) type @param string $data . . It's mean parameter is string(single parameter ) type

interface ProductsInterface 
{
    /**
     * Returns greeting message to user
     *
     * @api
     * @param array $data   ========== Assign type array to your $data param variable
     * @return array.
     */
    public function getProductPrice($data);
}

When you pass value of param in your rest api, pass data in json array format like {"data":{"product_sku":"Pro-1", "product_id":77}}.

Login Section use parameter like.

class Products implements ProductsInterface
{

    public function getProductPrice($data)
    {
        /*$data['product_sku']; $data['product_id'];*/
        $prodcutBySku = $this->_productRepository->get($data['product_sku']);
        return $prodcutBySku->getPrice();
   }
}
3
  • what do you mean, i did not understand
    – sudo55
    Jun 20, 2019 at 11:45
  • Means you have to set array type for your param in comment section as "* @param array $data". And when you call your api and passing your param, pass it in json array format like {"data":{"product_sku":"Pro-1", "product_id":77}} Jun 20, 2019 at 11:58
  • Yes @DharaBhatti . I have just explain concept of How to pass array type parameter into Rest API and How to use parameter into login . edit post please review it.
    – Shorabh
    Jun 20, 2019 at 12:01
0

simply Add code for Multiple Parameter pass in your web.xml file

<route url="/V1/products/getProductAttribute/:sku/:attributecode" method="GET">
       <service class="Evamp\Webapi\Api\ProductsInterface" method="getProductAttribute"/>
        <resources>
          <resource ref="anonymous"/>
        </resources>
</route>

Note: the parameter name Must be same in Abstract Method in Interface File and Must be same in Method Implement File.

I Hope This Helps You.

1
  • I tried multiple parameter like your answer. But it cause Type Error occurred when creating object: Magento\Framework\Communication\Config\Data error. Did you have any solution for this error? Jun 1, 2020 at 4:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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