Server API post JSON data array

Your json is not valid…remove the comma and it should work.

Facepalm. That’s what you get when a non-coder starts fiddling with code :smiley:

{
  "client_id": "test123"
}

Produces succes.

[
{
  "client_id": "test123"
}
]

Produces this in webhook.site:

"[{\n \"client_id\": \"test123\"\n}]"

Is that supposed to happen? I’m just keeping it real simple for now, just to figure it out.

That is json with escaped values so probably fine.

It’s not easy to test/debug when it’s formatted like that, but I’m not sure what is going wrong here. This is what I did in Postman, which works fine:

When I first create the server action in Wappler, I enter exactly the same data:

But when I press refresh within Wappler (in the server actions panel), suddenly this screen looks like this:

I then cannot enter a query/header anymore. I can still run this server action, but it produces an error from the API destination. While it’s exactly the same code as far as I can see.

1. status: "ERROR"
2. message: "An array of records is expected"

Ya, this is not looking right. If it were me, I would start from scratch to avoid the back and forth of trying different things. Just start a new server action to troubleshoot.

Yes that’s what I did in the above example. I have a bug report here, so hopefully the team can tackle it

Hi @nevil,

I’m aware of the query & header field going unresponsive and needs to be fixed.

Try this and let me know if it works:

  1. Enclose the whole json data with {{ }} and assign it to a variable. When pasting this data to the variable, remove all line & para breaks (I use this online page - https://www.textfixer.com/tools/remove-line-breaks.php). When you paste it to the variable (pencil icon), it should automatically classify this as “text” data & not “json” (it is still json within)
Set Value json_data = {{
[
    {
        "client_id": 113974,
        "table_name": "customers",
        "sequence": 1565881320,
        "key_names": [
            "id"
        ],
        "data": {
            "id": 1,
            "name": "Finn"
        },
        "action": "upsert"
    }
]
}}
  1. In your api call, set Data Type to JSON & set the JSON Data to the above variable like this:

JSON Data: {{json_data}}

Passing it through the variable avoids the unresponsive query & header fields issue. But, not sure if it’s going to work for you, I had something like this and made it work after long struggle… Let me know how you go!

Okay I did like you said, created a variable json_data and put the JSON in between {{}}:

image

But it still creates the query/header issue:

Running the server action in the front-end produces this error:

{
   "code":0,
   "file":"\/var\/www\/html\/dmxConnectLib\/lib\/core\/Lexer.php",
   "line":70,
   "message":"Unexpected character \n at column 0 in expression {{\n[\n    {\n        \"client_id\": 113974,\n        \"table_name\": \"customers\",\n        \"sequence\": 1565881320,\n        \"key_names\": [\n            \"id\"\n        ],\n        \"data\": {\n            \"id\": 1,\n            \"name\": \"Finn\"\n        },\n        \"action\": \"upsert\"\n    }\n]\n}}",
   "trace":"#0 \/var\/www\/html\/dmxConnectLib\/lib\/core\/Parser.php(84): lib\\core\\Lexer->parse('\\n[\\n    {\\n      ...')\n#1 \/var\/www\/html\/dmxConnectLib\/lib\/App.php(221): lib\\core\\Parser->parse('\\n[\\n    {\\n      ...', NULL)\n#2 \/var\/www\/html\/dmxConnectLib\/lib\/App.php(206): lib\\App->parseObject('{{\\n[\\n    {\\n    ...', NULL)\n#3 \/var\/www\/html\/dmxConnectLib\/modules\/core.php(99): lib\\App->parseObject(Object(stdClass))\n#4 \/var\/www\/html\/dmxConnectLib\/lib\/App.php(173): modules\\core->setvalue(Object(stdClass), 'json_data')\n#5 \/var\/www\/html\/dmxConnectLib\/lib\/App.php(137): lib\\App->execSteps(Object(stdClass))\n#6 \/var\/www\/html\/dmxConnectLib\/lib\/App.php(107): lib\\App->execSteps(Array)\n#7 \/var\/www\/html\/dmxConnectLib\/lib\/App.php(72): lib\\App->exec(Object(stdClass))\n#8 \/var\/www\/html\/dmxConnect\/api\/Sync\/QLS\/json_test3.php(8): lib\\App->define(Object(stdClass))\n#9 {main}"
}

Hi @nevil,

If you removed the line breaks & para breaks as per my above suggestion and then assign your data to the variable, your variable will look like this (it will go into one line and you scroll horizontally). It will automatically classify it as “Text” on the top right corner.

image

Try this. When I pasted your json within double curly brackets in my previous reply, the forum’s block tool automatically beautified the json.

Your json without line & para breaks will look like this:

{{ [ { “client_id”: 113974, “table_name”: “customers”, “sequence”: 1565881320, “key_names”: [ “id” ], “data”: { “id”: 1, “name”: “Finn” }, “action”: “upsert” } ] }}

I have a similar setup and it works at my end, but every api is different. I inferred that wappler still has to perfect the newly introduced json data thingy, and the above text method is a workaround till then…

In the newest version 2.5.6 (just released few minutes ago) it gives me this error:

image

This is a new validation on Wapplers side I think @Teodor ? It does allow me to save the action file (I see the green notification on the right), but it doesn’t save the JSON data field text I input there.

Hello Nevil,
This input expects you to paste your JSON directly there.

Hi @Teodor ,

This breaks how things were working normally earlier. The JSON data might come from another api source (or) may be built using various data on server side (or) pulled from a database (or) might be an input from the client-side.

If the json data can’t be passed through a variable in the API call (which worked fine till last version), then it will break my current site and I’m guess many other’s as well.

Ah yes sorry, my mistake - it should accept dynamic data indeed.
@George will check this.

1 Like

try to enclose it in double quotes, like:

"{{json_data}}"

Pasting my JSON directly in there doesn’t work also, it outputs the following:

"[\n {\n \"client_id\": 113974,\n \"table_name\": \"customers\",\n \"sequence\": 1565881320,\n \"key_names\": [\n \"id\"\n ],\n \"data\": {\n \"id\": 1,\n \"name\": \"Finn\"\n },\n \"action\": \"upsert\"\n }\n]"

While it should output this:

[
    {
        "client_id": 113974,
        "table_name": "customers",
        "sequence": 1565881320,
        "key_names": [
            "id"
        ],
        "data": {
            "id": 1,
            "name": "Finn"
        },
        "action": "upsert"
    }
]

Tried that one too, but then the entire server action fails and gives a 500 error.

I really think wappler doesn’t handle the array’s well in this specific case.

what is the server error that you get when you enclose your dynamic expressions in quotes?
That should always be done within JSON btw.

There is no dynamic data (just testing static data for now).

Using Postman:

Using Wappler:

But that’s because Wappler outputs it like this, which Postman doesn’t do:

"[\n {\n \"client_id\": 113974,\n \"table_name\": \"customers\",\n \"sequence\": 1565881320,\n \"key_names\": [\n \"id\"\n ],\n \"data\": {\n \"id\": 1,\n \"name\": \"Finn\"\n },\n \"action\": \"upsert\"\n }\n]"

I think I found the problem - is indeed when you have JSON data starting as array, so with [ and ]

if it starts with { } as object it goes all fine.

So will investigate further

2 Likes

are you sure you need the [ and ] - because usually you are posting a single object with JSON …