# Send values to http post json with Homeyscript

**URL:** <https://community.homey.app/t/send-values-to-http-post-json-with-homeyscript/102358>\
**Category:** Questions & Help\
**Tags:** homeyscript\
**Created:** [January 26, 2024, 1:56pm UTC](https://community.homey.app/t/send-values-to-http-post-json-with-homeyscript/102358 "2024-01-26T13:56:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sundlof](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/sundlof/32/51033_2.png) [@Sundlof](https://community.homey.app/u/Sundlof)\
**Post date:** [January 26, 2024, 1:56pm UTC](https://community.homey.app/t/send-values-to-http-post-json-with-homeyscript/102358/1 "2024-01-26T13:56:41Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Andy\_Henderson](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/andy_henderson/32/80642_2.png) [@Andy\_Henderson](https://community.homey.app/u/Andy_Henderson)\
**Post date:** [January 26, 2024, 5:56pm UTC](https://community.homey.app/t/send-values-to-http-post-json-with-homeyscript/102358/2 "2024-01-26T17:56:24Z")

</div>

This bit of HomeyScript does the post part for me…

```auto
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

---

<div class="post-metadata">

**Author:** ![Andy\_Henderson](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/andy_henderson/32/80642_2.png) [@Andy\_Henderson](https://community.homey.app/u/Andy_Henderson)\
**Post date:** [January 26, 2024, 6:01pm UTC](https://community.homey.app/t/send-values-to-http-post-json-with-homeyscript/102358/3 "2024-01-26T18:01:49Z")

</div>

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

> [@HomeyScript: Any sane ways to use args?](https://community.homey.app/t/homeyscript-any-sane-ways-to-use-args/66178):
>
> Whenever you start a HomeyScript and pass it args, they’re all sent as a single string. So you have to use some sort of delimiter. I changed from using space to using #, since that’s less likely to occur. So all scripts have to start with the boiler plate: let myArgs = args.split('#'); let foo = parseInt(myArgs[0]); let bar = parseFloat(myArgs[1]); Anyone else have a better way of doing it?

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [January 26, 2024, 6:02pm UTC](https://community.homey.app/t/send-values-to-http-post-json-with-homeyscript/102358/4 "2024-01-26T18:02:33Z")

</div>

That’s doing a GET, not a POST 😅

To POST data as JSON with HomeyScript:

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

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

```

---

<div class="post-metadata">

**Author:** ![Sundlof](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/sundlof/32/51033_2.png) [@Sundlof](https://community.homey.app/u/Sundlof)\
**Post date:** [February 3, 2024, 9:57am UTC](https://community.homey.app/t/send-values-to-http-post-json-with-homeyscript/102358/5 "2024-02-03T09:57:39Z")

</div>

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!
