Working with server side API form data

The API i am currently using want form data submitted as JSON like this

{
  "status": "{{$_POST.status}}",
  "teststring": "{{$_POST.teststring}}",
  "testboolean": "{{$_POST.testboolean}}",
  "testint": "{{$_POST.testint}}"
}

The problem I am facing is that it works great only if all the data is completed, i.e. every field becomes a required field. I have tried a few ways to make this conditional, as in only if something has been completed in that field then send it, however I am not really coming right.

Anyone have any suggestions on this.

Is your API endpoint expecting all four fields mandatory? If that’s the case, then you are limited by that, isn’t it? Sorry, I might have misunderstood.

No the API endpoint could take only one of them if i like, however by doing it in this JSON style like it wants if I only enter details in one it fails.

So, as the JSON structure is static, some fields might be null, and the api endpoint doesn’t like it?

{
“status”: “active”,
“teststring”: null
“testboolean”: null,
“testint”: 1001
}

Unlike app-connect front-end (variables, arrays & datastore/json component), there are no such components on the server-connect side, hence this challenge…

I had a tough time building dynamic json structure and eventually made it work… Gimme some time, I will draft & share how I achieved it…

1 Like

The App Connect side does not have Patch, Delete etc. which I know would be silly anyway as I imagine all APIs would require tokens for anything more than data viewing, and we do not want the keys exposed for viewing, so I have to use the server side to get the functionality.

The strange thing is that the POST to add new data works with the server connect set to POST and the form fields as input parameters like this

Yet the update/PATCh method does not and has to take JSON like this

In my mind I kind of figured the Add Data and Update Data would be fairly similar.

Not even when you change the datatype field from JSON to Form?

1 Like

Sadly no I tried changing it between form and Jason and both ways I had the same error of the payload missing

Maybe @patrick can suggest a solution for this :slight_smile:

1 Like

I tried some ways, but such a simple trivial dynamic json processing, it looks like can’t be done out of the box without custom formatter… will have to rely on @patrick for this one…

Hopefully the Data Transformation action that George promised in the other thread can deliver this.

1 Like

The Data Transformation is not going to help, they are for collections (arrays). For objects we don’t have any formatters and there aren’t any planned at the moment. I will see if we can think of some formatters that would be helpful for manipulating objects.

In your case it is not a difficult one. Here a php function that would filter out the null values.

function formatter_cleanObject($val) {
  return (object)array_filter((array)$val, function($v) {
    return isset($v);
  });
}

I will check this and fix if needed.

2 Likes