Can't send JSON from Server Connect to external API

I got array with repeated data in Server action and I need to send it for external API .
But I receive only empty array as result.



Perhaps I am describing the JSON structure incorrectly, or do I need to process records somehow in a loop?

You need to process entries from Repeat setting your Query as an Repeat Data. Then you should use aas_id_amo etc in JSON Data editor with concotinating +{{aas_id_amo}}+

I don’t know if I understood your advice correctly - I created a ‘json’ array and in a loop I create separate objects for each element of the input data, but I did not find how to write regular commands here - for example, how to put a separate element at the end of the array?

Handling of arrays is still a bit tricky in Wappler, the team is working on making it easier

Meanwhile, I suggest inside the Repeat step you just do:

Set Value id = {{ass_id_amo}}
Set Value start = {{start_date}}
(...)

And in the Repeat step configure it so Output checkbox is ticked (so you can see the results in your browser’s developer console), and output no fields (of the original “abon”), so the only thing it’s going to output are your Set Values above

The execution of Repeat returns an array, so you could use the result of the Repeat in the API Action

Seems I now correctly have repeat values, but in API action it not send normal. What is the correct data format to use in a POST request?

Just {{repeat[0]}} to send the first item

You can use a service like Beeceptor to see what data you’re sending

1 Like

At me there the empty array is sent. How can I use Repeat in query parameters correctly to send all elements of an array?

Check Output checkbox on each Set Value inside the Repeat

Everything is fine with the data in Repeat, but there is a problem with sending this data to the API at the last step, it seems that need to set parameters in a POST request somehow differently?



I found the reason. Wappler sent this request in such a way that in PHP it is only available via file_get_contents(‘php://input’), not via $_POST.
This is a little strange, since normal POST requests to the frontend action API come in fine in $_POST

The $_POST variables in PHP are only filled when the request was using the Content-Type application/x-www-form-urlencoded or multipart/form-data. In your case you posted the data as JSON with the Content-Type application/json, this is not parsed by the PHP server and you will need to parse it yourself.

PHP: $_POST - Manual

In Server Connect we support JSON posts and it is getting parsed and will fill the $_POST variables.

A stripped version of how it is implemented in Server Connect.

$contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : '';

$post = $_POST;

if (stripos($contentType, 'application/json') === 0) {
  $raw = remove_utf8_bom(file_get_contents('php://input'));
  $post = json_decode($raw, TRUE);
}

function remove_utf8_bom($text) {
  $bom = pack('H*','EFBBBF');
  $text = preg_replace("/^$bom/", '', $text);
  return $text;
}
1 Like

Thanks Patrick!

I used similar code, but yours is clearly better, more versatile :slight_smile: