Send values to http post json with Homeyscript

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?


This bit of HomeyScript does the post part for me…

let json = JSON.stringify(deviceData);
let queryString = encodeURIComponent(json);
let request = '' + queryString; 
let result = await fetch(request);
let message = 'Sent: ' + request;
if (!result.ok) {
  message = Error(result.statusText);

The logging is for debug purposes.


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.