Tuesday, August 8, 2017

Magento 2 Rest Api - Associative Array In Request Param

Leave a Comment

I am trying to create custom Api in magneto2. Its a POST CALL, in which i am trying to send associative array like below,

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

For the above format i am giving the param annotation as string[]. Its working If i didn't pass place data in it. But if i am passing place data its throwing error like below,

Array to string conversion 

Actual problem is i am not able to pass associative array in request param. Can anyone please help me on this?

I tried by creating an custom object type also. In that too i can give string[] type only. So i cannot add more and more json data in it.

1 Answers

Answers 1

you can create array in PHP and use function to encode to json for e.g.

    $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!

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment