Magento 2 Rest Api - associative array in Param request

I am trying to create a custom Api in magneto2. Its a POST CALL in which I try to send an associative array as shown below,

{ "data": { "testData": { "title": "Test 01", "place": { "key": "value" } } } } 

In the above format, I give the param annotation as string[] . Its work If I did not pass place data to it. But if I pass the place data my throwing error, as shown below,

 Array to string conversion 

Actual problem: I cannot pass an associative array in the query parameter. Can someone help me with this?

I also tried to create a custom object type. In this, too, I can only give the type string[] . Therefore, I cannot add more and more json data to it.

+5
source share
1 answer

you can create an array in php and use a function to encode json for example

  $arr= array( 'val1'=>array('your val1', 'your val2'), 'val2'=>array('your val1', 'your val2'), ); $data = json_encode($arr); // if you want to set in url $client = new Zend_Http_Client($uri); $client->setRawData($data, 'application/json')->request('POST'); 

you will need to use json_decode to get your values โ€‹โ€‹later. Hope this helps!

0
source

Source: https://habr.com/ru/post/1270282/


All Articles