0

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="Meridian\TopMarken\Api\TopMarkenRepositoryInterface" 
           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 \Meridian\TopMarken\Api\Data\TopMarkenInterface
     */
    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 2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.