Print

Zoho Deluge: Post a multi-dimensional or nested array to a 3rd-party API

What?
This is an article documenting how to send a Zoho Deluge Map request to an API which is expecting nested/2d/multi-dimensonal arrays. In the example below, we are posting to a PHP script for testing but I have since sent something similar to an API expecting nested arrays (not sure if they were PHP) and it works.

Why?
This took me several days and in the end only going through various forums and documentation, I found a solution which worked for me. Note that usually I could customize the receiving PHP script to receive a JSON request and process it that way but my usage was to send data to a Third-Party API over which I had no control.

What I need to send to the 3rd-party API:
copyraw
{
  "auth": {
    "secret": "can_you_keep_a_secret",
    "key": "aaabbbbccccddddeeeeffff11112222"
  },
  "data": {
    "member_id": "123456",
    "channel_name": "shopify",
    "item_name": "Joel's Awesome Life",
    "item_price": "priceless"
  }
}
  1.  { 
  2.    "auth": { 
  3.      "secret": "can_you_keep_a_secret", 
  4.      "key": "aaabbbbccccddddeeeeffff11112222" 
  5.    }, 
  6.    "data": { 
  7.      "member_id": "123456", 
  8.      "channel_name": "shopify", 
  9.      "item_name": "Joel's Awesome Life", 
  10.      "item_price": "priceless" 
  11.    } 
  12.  } 
What Zoho sends to the 3rd-party API:
Sending a map request using the invokeUrl() function, the odd thing is that Zoho's request adds double-quotes to the request and escapes the other double-quotes sending something like this:
copyraw
{
  "auth": "{\"secret\": \"can_you_keep_a_secret\",\"key\":\"aaabbbbccccddddeeeeffff11112222\"}",
  "data": "{\"member_id\": \"123456\",\"channel_name\": \"shopify\",\"item_name\": \"Joel's Awesome Life\",\"item_price\": \"priceless\"}"
}
  1.  { 
  2.    "auth": "{\"secret\": \"can_you_keep_a_secret\",\"key\":\"aaabbbbccccddddeeeeffff11112222\"}", 
  3.    "data": "{\"member_id\": \"123456\",\"channel_name\": \"shopify\",\"item_name\": \"Joel's Awesome Life\",\"item_price\": \"priceless\"}" 
  4.  } 
What I have in Deluge
copyraw
m_Auth = Map();
m_AuthSub = Map();
m_AuthSub.put("secret","can_you_keep_a_secret");
m_AuthSub.put("key","aaabbbbccccddddeeeeffff11112222");
m_Auth.put("auth",m_AuthSub);
m_Data = Map();
m_DataRecord = Map();
m_DataRecord.put("member_id","123456");
m_DataRecord.put("channel_name","shopify");
m_DataRecord.put("item_name","Joel's Awesome Life");
m_DataRecord.put("item_price","priceless");
m_Data.put("data", m_DataRecord);
  1.  m_Auth = Map()
  2.  m_AuthSub = Map()
  3.  m_AuthSub.put("secret","can_you_keep_a_secret")
  4.  m_AuthSub.put("key","aaabbbbccccddddeeeeffff11112222")
  5.  m_Auth.put("auth",m_AuthSub)
  6.  m_Data = Map()
  7.  m_DataRecord = Map()
  8.  m_DataRecord.put("member_id","123456")
  9.  m_DataRecord.put("channel_name","shopify")
  10.  m_DataRecord.put("item_name","Joel's Awesome Life")
  11.  m_DataRecord.put("item_price","priceless")
  12.  m_Data.put("data", m_DataRecord)

How?
So this is more of an annoyance then an impossible task... because it is possible, the technique above is simply wrong. The trick and solution is a lot simpler than you might think... Doing the above, I would get a really messy response (I'm targeting an API made with ExpressionEngine) such as:

Fatal error: Uncaught Error: Class '\Webservice\Service\Exception' not found in /home/thirdpartyapi/public_html/thirdpartyapi_controlpanel/user/addons/thirdpartyapi_webservice/Service/Format.php:86 Stack trace: #0 /home/thirdpartyapiorg/public_html/thirdpartyapi_controlpanel/user/addons/thirdpartyapi_webservice/Service/Format.php(66): thirdpartyapi\Webservice\Service\Format->__construct('auth[key]=06cda...', 'php') #1 /home/thirdpartyapiorg/public_html/thirdpartyapi_controlpanel/user/addons/thirdpartyapi_webservice/libraries/services/webservice_rest.php(206): thirdpartyapi\Webservice\Service\Format->factory('auth[key]=06cda...', 'php') #2 /home/thirdpartyapiorg/public_html/thirdpartyapi_controlpanel/user/addons/thirdpartyapi_webservice/libraries/services/webservice_rest.php(149): Webservice_rest->call_method('create_entry') #3 /home/thirdpartyapiorg/public_html/thirdpartyapi_controlpanel/user/addons/thirdpartyapi_webservice/Core/Hook/SessionsStart.php(106): Webservice_rest->__construct() #4 /home/thirdpartyapiorg/public_html/thirdpartyapi_controlpanel/user/addons/thirdpartyapi_webservice/ext.thirdpartyapi_webservice.php(8 in /home/thirdpartyapiorg/public_html/thirdpartyapi_controlpanel/user/addons/thirdpartyapi_webservice/Service/Format.php on line 86
Rather than assuming Zoho Deluge maps and lists are JSON variables, let's send a strange hack instead:
copyraw
m_NestedRequest = Map();
m_NestedRequest.put("auth[key]",v_AuthKey);
m_NestedRequest.put("auth[secret]",v_AuthSecret);
m_NestedRequest.put("data[member_id]","123456");
m_NestedRequest.put("data[channel_name]","shopify");
m_NestedRequest.put("data[item_name]","Joel's Awesome Life");
m_NestedRequest.put("data[item_price]","priceless");
r_PostResponse = invokeurl
[
    url :"https://api.joellipman.com/my_test_api.php"
    type :POST
    parameters:m_NestedRequest
];
  1.  m_NestedRequest = Map()
  2.  m_NestedRequest.put("auth[key]",v_AuthKey)
  3.  m_NestedRequest.put("auth[secret]",v_AuthSecret)
  4.  m_NestedRequest.put("data[member_id]","123456")
  5.  m_NestedRequest.put("data[channel_name]","shopify")
  6.  m_NestedRequest.put("data[item_name]","Joel's Awesome Life")
  7.  m_NestedRequest.put("data[item_price]","priceless")
  8.  r_PostResponse = invokeUrl 
  9.  [ 
  10.      url :"https://api.joellipman.com/my_test_api.php" 
  11.      type :POST 
  12.      parameters:m_NestedRequest 
  13.  ]

The receiving PHP script did however understand this hack as:
copyraw
$v_Key = "aaabbbbccccddddeeeeffff11112222";
$v_Secret = "can_you_keep_a_secret";
$v_PostedKey = "-";
$v_PostedSecret = "-";
if(isset($_POST['auth']))
{
    if(isset($_POST['auth']['key']))
    {
        $v_PostedKey = $_POST['auth']['key'];
    }
    if(isset($_POST['auth']['secret']))
    {
        $v_PostedSecret = $_POST['auth']['secret'];
    }
    if($v_PostedKey == $v_Key && $v_PostedSecret == $v_Secret)
    {
        mail("This email address is being protected from spambots. You need JavaScript enabled to view it.","Post to AscentCloud","Successful Auth");
    }
}
// yields: Successful Auth
  1.  $v_Key = "aaabbbbccccddddeeeeffff11112222"
  2.  $v_Secret = "can_you_keep_a_secret"
  3.  $v_PostedKey = "-"
  4.  $v_PostedSecret = "-"
  5.  if(isset($_POST['auth'])) 
  6.  { 
  7.      if(isset($_POST['auth']['key'])) 
  8.      { 
  9.          $v_PostedKey = $_POST['auth']['key']
  10.      } 
  11.      if(isset($_POST['auth']['secret'])) 
  12.      { 
  13.          $v_PostedSecret = $_POST['auth']['secret']
  14.      } 
  15.      if($v_PostedKey == $v_Key && $v_PostedSecret == $v_Secret) 
  16.      { 
  17.          mail("This email address is being protected from spambots. You need JavaScript enabled to view it.","Post to AscentCloud","Successful Auth")
  18.      } 
  19.  } 
  20.  // yields: Successful Auth 

Caveat
Unfortunately, I've titled this article as multi-dimensional but I found that this code doesn't support an array nested more than 2 levels deep. In other words, the above solution will not work if I wanted to post the following:
copyraw
{
  "auth": {
    "secret": {
        "word":"can_you_keep_a_secret",
        "fact":"Elephants are the world's largest land animal",
    "key": "aaabbbbccccddddeeeeffff11112222"
  }
}
// using
m_NestedRequest = Map();
m_NestedRequest.put("auth[key]",v_AuthKey);
m_NestedRequest.put("auth[secret][word]","can_you_keep_a_secret");
m_NestedRequest.put("auth[secret][fact]","Elephants are the world's largest land animal");
//
// FAILS !!!  $_POST['auth']['secret']['word'] is NOT set
  1.  { 
  2.    "auth": { 
  3.      "secret": { 
  4.          "word":"can_you_keep_a_secret", 
  5.          "fact":"Elephants are the world's largest land animal", 
  6.      "key": "aaabbbbccccddddeeeeffff11112222" 
  7.    } 
  8.  } 
  9.  // using 
  10.  m_NestedRequest = Map()
  11.  m_NestedRequest.put("auth[key]",v_AuthKey)
  12.  m_NestedRequest.put("auth[secret][word]","can_you_keep_a_secret")
  13.  m_NestedRequest.put("auth[secret][fact]","Elephants are the world's largest land animal")
  14.  // 
  15.  // FAILS !!!  $_POST['auth']['secret']['word'] is NOT set 

Source(s):
Category: Zoho :: Article: 740