5

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.

Vinoth Babu
  • 6,724
  • 10
  • 36
  • 55

1 Answers1

0

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!

Pallavi
  • 347
  • 1
  • 4
  • 12