Setting server side object variables

Hi All,

I have two questions, both related to server side actions.

  1. How do I assign values to variables contained within a session object. In other words, in the screenshot below, what is the syntax for “name” in set session properties, for test[var1]?

23%20PM

  1. Similarly, how do I send complex objects via the API action?

For example, with this json body to be sent:

{
	"client_id": "{{client_id}}",
	"secret": "{{secret_key}}",
	"access_token": "ENTER_ACCESS_TOKEN_HERE",
	"start_date": "2017-01-01",
	"end_date": "2018-01-01",
	"options": {
		"count": 250,
		"offset": 100
	}
}

How do I format the input data value for “options”? I can successfully send to the api without the options value, but can not find the right syntax for the nested object.

45%20PM

Appreciate any help here!

–Ken

1 Like

Any thoughts? The first item isn’t a blocker, but sending nested json to the api is. Hope there is a solution!

Try pls
Options[{{$index}}][count]
Options[{{$index}}][offset]

Thanks @s.alpaslan. I’m trying to call an api, and send json, not evaluate a response containing json. I’m looking for how to build nested elements in a server api POST action and send that json.

{
  "options": [
    {
      "count": "20",
      "offset": "100"
    }
  ],
  "client_id": "123",
  "secret": "test_secret",
  "access_token": "test",
  "start_date": "2019-01-01"
}

is this result correct / enought for you ?

use this {{$_POST.options}}
and field name must be an array like Options[{{$index}}][count] in repeater

or Options[0][count] / Options[0][offset]

it will work perfect …

and you can modify the server action file so you can just use {{$_POST}}

"options": {
        "url": "https://aaa.requestcatcher.com/",
        "dataType": "json",
        "data": {{$_POST}},
        "method": "POST"
      },
      "output": true

Thanks @s.alpaslan.

I’m actually triggering all this server-side in response to a webhook, so there isn’t any opportunity to build in with $_POST.

I finally realized however, that I can just edit my server api action in the editor and build the nested json I need! So, problem solved, but it would be nice at some point to be able to build these in the gui editor.

It ends up looking like this:

I was able to use the editor in order to get the “count” and “offset” elements added to the json, and this works great. I have one last piece though, that I can’t decipher.

I need to add the “account_ids” which is a list of strings. These values will come from a db query.

{
	"client_id": "{{client_id}}",
	"secret": "{{secret_key}}",
	"access_token": "ENTER_ACCESS_TOKEN_HERE",
	"start_date": "2017-01-01",
	"end_date": "2018-01-01",
	"options": {
		"count": 250,
		"offset": 100,
		"account_ids": ["223984798DJEKL", "SHLEI290500S"]
	}
}

I’m assuming I use some form of {{queryGetAccounts.account_ids}} but haven’t found anything that works in json.

Anybody know how do I reference a db query array, and maintain a proper json request?

@patrick can you help with this last piece? How to get a list into an api action input.

If {{queryGetAccounts.account_ids}} return the exact data needed then you can use it, just put it in a string like "account_ids": "{{queryGetAccounts.account_ids}}". If it is not in the correct format then perhaps one of the formatters can help, if you are missing a formatter then let us know.

Unfortunately, that returns null.

From the wappler editor, the api action (note, I had account_ids instead of account_id originally for the db query results, which was a typo):

Here’s the raw capture from webhook.site:

And here is what queryGetAccounts is returning:

Thanks for the help!

try {{queryGetAccounts.groupBy('account_id').keys()}}

Success!

"options": {
              "count": 500,
              "offset": 0,
              "account_ids": "{{queryGetAccounts.groupBy('account_id').keys()}}"
            }

Thanks Patrick…very much appreciate your time on this.

I’m exactly in the same scenario, any help will be greatly appreciated, stuck on this for almost two days :frowning:

I have constructed a custom nested json {{query_usersignals}} which I send as input data for my POST api call. Format was suggested by @George in @nevil’s thread here - Send nested JSON with server-side API. This whole json is constructed at server side only (not fetched through client POST) and my elastic search api needs needs this nested json (query DSL).

image

This is my nested json object {{query_usersignals}}. The below whole structure is further enclosed within {{ }} as suggested by George in the other thread.

{
        "bool": {
            "must": [
                {
                    "match": {
                        "userID": "qatest"
                    }
                }
            ]
        }
    }

I’m so glad to get this work. I can see the api query returning data for this userID “qatest” (hard-coded) in my browser. I expanded the results and verified that the above input data filter did work, so pretty good outcome so far.

Now, where I’m unable to progress anymore is when I wanted to pass the userID variable {{username}} in the above nested json. I did open the server action in the editor and tried replacing the hard-coded value (\“qatest\”) with the {{username}} variable. Tried various variants, removing quotes, etc, but always hitting a format error and I end up breaking this server action.

Too sleep deprived, would be great if the community can help. @Teodor @patrick @mebeingken

In the Set Value step remove the outer {{ }} from the value. So it would become:

{\"bool\":{\"must\":[{\"match\":{\"zorroId\":\"qatest\"}}]}}

or when using a expression then you need extra quotes:

{{'{\"bool\":{\"must\":[{\"match\":{\"zorroId\":\"qatest\"}}]}}'}}

Hi @patrick, thanks, thing is this expression is working fine… as it is… Removing the outer {{ }} breaks the api call.

What I wanted is, in this json, replace value “qatest” with a variable called {{username}}. When I try doing this by opening the server action php file in the editor, I get a formatting error on this page. Are you able to help me here please?

This is similar to what @nevil tried here - Send nested JSON with server-side API. And what @mebeingken tried in the above thread.

Do you get an error when replacing “qatest” with username?

{{{ \"bool\": { \"must\": [ { \"match\": { \"zorroId\": username } } ] } }}}
1 Like

Thanks Patrick! Feeling soo relieved that this is fixed… learning slowly with wappler, getting better day by day, thanks again!

In Wappler 2.3.4 it’s now possible to use nested JSON data structures directly in the Server API Connector UI.

2 Likes

This is simply amazing, thanks @Teodor