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:
{
  "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"
  }
}
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:
{
  "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\"}"
}
What I have in Deluge
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);

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:
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
];

The receiving PHP script did however understand this hack as:
$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

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:
{
  "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

Source(s):

Related Articles

Joes Revolver Map

Accreditation

Badge - Certified Zoho Creator Associate
Badge - Certified Zoho Creator Associate

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Paypal:
Donate to Joel Lipman via PayPal

Bitcoin:
Donate to Joel Lipman with Bitcoin - Valid till 8 May 2022 3QnhmaBX7LQSRsC9hh6Je9rGQKEGNQNfPb
© 2021 Joel Lipman .com. All Rights Reserved.