Send values to http post json with Homeyscript

Hi,
Today I use app HTTP request flow cards to send (POST) Json data to a website (PHP). But with the growing need for a lot of values, this gets messy. For example by writing the json in the small text field.

Is there any way this can be done with Homeyscript instead? Does anyone have the card “POST JSON” (A31 - Execute a POST with JSON data. Parameters: 1. Url; 2. JSON data.) in the format of Homeyscript so I can write my own and send all of the values this way?

Thanks!

This bit of HomeyScript does the post part for me…

let json = JSON.stringify(deviceData);
log(json);
let queryString = encodeURIComponent(json);
log(queryString);
let request = 'http://pageaddress.com/?parameter=' + queryString; 
let result = await fetch(request);
let message = 'Sent: ' + request;
if (!result.ok) {
  message = Error(result.statusText);
}
log(message);

The logging is for debug purposes.

Andy

There’s a discussion about passing arguments to HomeyScript here…

That’s doing a GET, not a POST :sweat_smile:

To POST data as JSON with HomeyScript:

const data = { foo : 'bar', hello : 'world' };

await fetch(URL, {
  method:  'POST',
  headers: { 'content-type' : 'application/json' },
  body:    JSON.stringify(data)
});

Thanks, manage to get everything to work with this!

Had some issues with all the “capability.lastUpdated” that where all timezone UTC when neither my Homey or the server thet receives the data is in UTC. But since this is a function just for me I adjust accordingly in the code.

Thanks!