skip to Main Content

Magento 2 custom rest api,I can not get an array in modalrepository,

How can i pass an array as parameter ?

Webapi.xml

    <route method="POST" url="/V1/topmarkens/productlists">
        <service class="MeridianTopMarkenApiTopMarkenRepositoryInterface" 
           method="productFilterByBrand"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
        <data>
            <parameter name="types" force="true">%types%</parameter>
        </data>
    </route>

TopMarkenRepositoryInterface.php

    /**
     * return placed order status
     * @param string[] $types
     * @return MeridianTopMarkenApiDataTopMarkenInterface
     */
    public function productFilterByBrand($types);

TopMarkenRepository.php

   public function productFilterByBrand($types){
        $vendor_data = json_decode($types);

        echo "<pre>"; print_r($vendor_data); die;

        return $vendor_data;
    }

2

Answers


  1. Directly sending an array as a parameter is not a good way of doing a POST request. I suggest you to send a json object in which u can have array as an first element of your object.

    Login or Signup to reply.
  2. I have this json:

    {
       "customerEmail":"[email protected]",
        "items": [
            {"sku":"sku-1", "qty":"1"},
            {"sku":"sku-2", "qty":"2"}
        ]
    }
    

    and this is my interface:

    interface OrderInterface
    {
        /**
         * @param string $customerEmail
         * @param mixed $items
         * @return bool
         * @api
         */
        public function create(string $customerEmail, $items) : bool;
    }
    

    Using string[] or array or mixed[] didn’t work out for me. Only mixed

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search