# Help with Homeyscript!

**URL:** <https://community.homey.app/t/help-with-homeyscript/43340>\
**Category:** Questions & Help\
**Created:** [February 20, 2021, 8:20am UTC](https://community.homey.app/t/help-with-homeyscript/43340 "2021-02-20T08:20:29Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Exile82](https://avatars.discourse-cdn.com/v4/letter/e/8491ac/32.png) [@Exile82](https://community.homey.app/u/Exile82)\
**Post date:** [February 20, 2021, 8:20am UTC](https://community.homey.app/t/help-with-homeyscript/43340/1 "2021-02-20T08:20:29Z")

</div>

Hi! With the Homeyscript I can see new possibilities, but I am totally useless on this, appearently! I have no background in programming, but have a good sense of how it works and what can be done. With that being said, I am in desperate need of assistance.  
Background: Me and some friends are in to photographing and flying drones, and share our work. Currently, we are using a P2P SW for sharing with automatic downloads. I would like to get notified when AirDC (SW) starts a new download.  
AirDC have a good API library that I intend to use, the one I think I should use is this [one](https://airdcpp.docs.apiary.io/#reference/queue/bundles/get-bundles)

The javascript (I guess javascript is okay) looks like this:

 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/a/2/a232b581acb5102eeda743726b20faa25e94c17e.png)

I would like to execute this script frequently and if a new download is added, I want to return the attribute “name” of the folder and via Homey create a push notification:

 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/4/7/472aef9b3aca8d9cf40eb9509a364660d5a496d5.png)

Please, I need some input if I am on the right track here

---

<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:** [February 20, 2021, 8:23am UTC](https://community.homey.app/t/help-with-homeyscript/43340/2 "2021-02-20T08:23:35Z")

</div>

Seems to me it’s much easier for you to use the [HTTP request flow cards app](https://homey.app/en-gb/app/com.internet/HTTP-request-flow-cards/).

---

<div class="post-metadata">

**Author:** ![Exile82](https://avatars.discourse-cdn.com/v4/letter/e/8491ac/32.png) [@Exile82](https://community.homey.app/u/Exile82)\
**Post date:** [February 20, 2021, 8:54am UTC](https://community.homey.app/t/help-with-homeyscript/43340/3 "2021-02-20T08:54:22Z")

</div>

Thanks @robertklep, thank you for taking the time to read my post and respond.  
At this point, I am very open for any solution! I actually have that app, so why not?! 😉

I guess I can use the Javascript as is?

---

<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:** [February 20, 2021, 9:04am UTC](https://community.homey.app/t/help-with-homeyscript/43340/4 "2021-02-20T09:04:21Z")

</div>

`XMLHttpRequest` won’t work in Homeyscript, you need to use [`fetch`](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch).

---

<div class="post-metadata">

**Author:** ![Exile82](https://avatars.discourse-cdn.com/v4/letter/e/8491ac/32.png) [@Exile82](https://community.homey.app/u/Exile82)\
**Post date:** [February 20, 2021, 9:26am UTC](https://community.homey.app/t/help-with-homeyscript/43340/5 "2021-02-20T09:26:52Z")

</div>

Thanks!  
I hope it supports fetch.  
I will test this and see if I get any wiser!

---

<div class="post-metadata">

**Author:** ![Exile82](https://avatars.discourse-cdn.com/v4/letter/e/8491ac/32.png) [@Exile82](https://community.homey.app/u/Exile82)\
**Post date:** [February 20, 2021, 9:31am UTC](https://community.homey.app/t/help-with-homeyscript/43340/6 "2021-02-20T09:31:22Z")

</div>

request.send(); does not seem to work in Homeyscrit either  
Any ideas?

---

<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:** [February 20, 2021, 9:32am UTC](https://community.homey.app/t/help-with-homeyscript/43340/7 "2021-02-20T09:32:25Z")

</div>

> [@Exile82](#):
>
> Any ideas?

Yes: look at the page about `fetch` that I linked to 😛 `fetch` has completely different calling semantics than `XMLHttpRequest`

---

<div class="post-metadata">

**Author:** ![Exile82](https://avatars.discourse-cdn.com/v4/letter/e/8491ac/32.png) [@Exile82](https://community.homey.app/u/Exile82)\
**Post date:** [February 20, 2021, 10:02am UTC](https://community.homey.app/t/help-with-homeyscript/43340/8 "2021-02-20T10:02:13Z")

</div>

Oh, sorry!  
I tries the respond.status just to see if I got any status (working/not working). But I only got this output:  
❌ Script Error  
⚠ ReferenceError: response is not defined  
at airdctest.js:11:1  
at processTicksAndRejections (internal/process/task\_queues.js:97:5)

Can I assume that this is not supported by AirDCs API?!

---

<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:** [February 20, 2021, 10:03am UTC](https://community.homey.app/t/help-with-homeyscript/43340/9 "2021-02-20T10:03:37Z")

</div>

No, it means your code isn’t correct.

I’ll subtly refer back to my [earlier post](https://community.homey.app/t/help-with-homeyscript/43340/2)… 😜

---

<div class="post-metadata">

**Author:** ![Exile82](https://avatars.discourse-cdn.com/v4/letter/e/8491ac/32.png) [@Exile82](https://community.homey.app/u/Exile82)\
**Post date:** [February 20, 2021, 10:26am UTC](https://community.homey.app/t/help-with-homeyscript/43340/10 "2021-02-20T10:26:43Z")

</div>

Yeah, thanks @robertklep!  
I know that it is like talking to a five year old. 🙂  
I have read the information, but problem is that I do not fully understand the mechanisms so I more or less pick parts on chance.

> var request = new fetch(‘[https://private-anon-3359b479e7-airdcpp.apiary-mock.com/queue/bundles/{0}/{100}](https://private-anon-3359b479e7-airdcpp.apiary-mock.com/queue/bundles/%7B0%7D/%7B100%7D)’);
> 
> request.onreadystatechange = Function(Response) {
> 
> if (this.readyState === 4) {
> 
> ```
> console.log('Status:', this.status);
> 
> console.log('Headers:', this.getAllResponseHeaders());
> 
> console.log('Body:', this.responseText);
> 
> ```
> 
> }
> 
> };
> 
> response.Headers()

---

<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:** [February 20, 2021, 10:43am UTC](https://community.homey.app/t/help-with-homeyscript/43340/11 "2021-02-20T10:43:33Z")

</div>

I’ll quote a part from the page I linked to:

> A basic fetch request is really simple to set up. Have a look at the following code:
> 
> ```auto
> fetch('http://example.com/movies.json')
> .then(response => response.json())
> .then(data => console.log(data));
> 
> ```

Like I said, `XMLHttpRequest` and `fetch` have completely different calling semantics. You just replaced `XMLHttpRequest` with `fetch`, but all the other code that’s left in place is specific to `XMLHttpRequest` and won’t work with `fetch`.

---

<div class="post-metadata">

**Author:** ![Exile82](https://avatars.discourse-cdn.com/v4/letter/e/8491ac/32.png) [@Exile82](https://community.homey.app/u/Exile82)\
**Post date:** [February 20, 2021, 10:54am UTC](https://community.homey.app/t/help-with-homeyscript/43340/12 "2021-02-20T10:54:36Z")

</div>

Thanks @robertklep! I really appreciate your support. I would like to emphasize that I am not lazy, I have read and tried and read some more before posting here.  
But when I don’t get it right I tried to get some help here. I will try this. Thanks again

---

<div class="post-metadata">

**Author:** ![Exile82](https://avatars.discourse-cdn.com/v4/letter/e/8491ac/32.png) [@Exile82](https://community.homey.app/u/Exile82)\
**Post date:** [February 21, 2021, 3:41pm UTC](https://community.homey.app/t/help-with-homeyscript/43340/13 "2021-02-21T15:41:23Z")

</div>

Well, I did some more reading, and some more testing. Are there someone that can help me translate the [example code](https://airdcpp.docs.apiary.io/#reference/queue/bundles/get-bundles) to something that works in Homeyscript or HTTP request flow cards?

> var request = new XMLHttpRequest();
> 
> request.open(‘GET’, ‘[https://private-anon-3c2bdff3aa-airdcpp.apiary-mock.com/queue/bundles/{start}/{count}](https://private-anon-3c2bdff3aa-airdcpp.apiary-mock.com/queue/bundles/%7Bstart%7D/%7Bcount%7D)’);
> 
> request.onreadystatechange = function () {  
> if (this.readyState === 4) {  
> console.log(‘Status:’, this.status);  
> console.log(‘Headers:’, this.getAllResponseHeaders());  
> console.log(‘Body:’, this.responseText);  
> }  
> };
> 
> request.send();

The attribute I want to return is “name”.

---

<div class="post-metadata">

**Author:** ![tb1962](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/tb1962/32/7888_2.png) [@tb1962](https://community.homey.app/u/tb1962)\
**Post date:** [February 21, 2021, 5:49pm UTC](https://community.homey.app/t/help-with-homeyscript/43340/14 "2021-02-21T17:49:48Z")

</div>

@Exile82, Look at the example that @robertklep gave to you. There is really not much more to do. The data object should contain the name attribute.

---

<div class="post-metadata">

**Author:** ![johan\_bendz](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/johan_bendz/32/10344_2.png) [@johan\_bendz](https://community.homey.app/u/johan_bendz)\
**Post date:** [February 21, 2021, 5:53pm UTC](https://community.homey.app/t/help-with-homeyscript/43340/15 "2021-02-21T17:53:09Z")

</div>

> [@robertklep](#):
>
> ```auto
> .then(response => response.json())
> .then(data => console.log(data));
> 
> ```

const url = “[https://private-anon-3c2bdff3aa-airdcpp.apiary-mock.com/queue/bundles/{start}/{count}](https://private-anon-3c2bdff3aa-airdcpp.apiary-mock.com/queue/bundles/%7Bstart%7D/%7Bcount%7D)”;

await fetch(url).then(data =\> data.json()).then(data =\> console.log(data[0].name));

---

<div class="post-metadata">

**Author:** ![johan\_bendz](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/johan_bendz/32/10344_2.png) [@johan\_bendz](https://community.homey.app/u/johan_bendz)\
**Post date:** [February 21, 2021, 6:03pm UTC](https://community.homey.app/t/help-with-homeyscript/43340/16 "2021-02-21T18:03:38Z")

</div>

// define url  
const url = “[https://private-anon-3c2bdff3aa-airdcpp.apiary-mock.com/queue/bundles/{start}/{count}](https://private-anon-3c2bdff3aa-airdcpp.apiary-mock.com/queue/bundles/%7Bstart%7D/%7Bcount%7D)”;  
// get data  
let data = await fetch(url);  
// make it json  
data = await data.json();  
// pick name from json data  
let name = data[0].name;  
// display it  
console.log(name);

---

<div class="post-metadata">

**Author:** ![Exile82](https://avatars.discourse-cdn.com/v4/letter/e/8491ac/32.png) [@Exile82](https://community.homey.app/u/Exile82)\
**Post date:** [February 21, 2021, 6:31pm UTC](https://community.homey.app/t/help-with-homeyscript/43340/17 "2021-02-21T18:31:13Z")

</div>

Thank you so much guys, now it turns up in console.log
