# Calling an external service and receiving the Response in a variable?

**URL:** <https://community.homey.app/t/calling-an-external-service-and-receiving-the-response-in-a-variable/12445>\
**Category:** Developers\
**Tags:** app, tools\
**Created:** [April 29, 2019, 4:36pm UTC](https://community.homey.app/t/calling-an-external-service-and-receiving-the-response-in-a-variable/12445 "2019-04-29T16:36:27Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jef\_van\_Leeuwen](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jef_van_leeuwen/32/9801_2.png) [@Jef\_van\_Leeuwen](https://community.homey.app/u/Jef_van_Leeuwen)\
**Post date:** [April 29, 2019, 4:36pm UTC](https://community.homey.app/t/calling-an-external-service-and-receiving-the-response-in-a-variable/12445/1 "2019-04-29T16:36:27Z")

</div>

a HTTP (POST) call in any browser to a external service returns the data (unixdate, Kelvin-temperatures and the moisture for a specific area i am interested in.

I want to make the call once or twice a day and put the response in the appropiate variables (better logic i.e.).

I expect that one of the HTTP calls in the HTTP app could do the job, but with so many cards and options i need some help/hint in a HowTo solution, anyone can Help ???

Example:  
Actual Call (i.e.):  
[http://api.agromonitoring.com/agro/1.0/soil?polyid=5cc5c0f3d86170003e090a62&appid=afb5821958544dc9fedba14e4e46f849](http://api.agromonitoring.com/agro/1.0/soil?polyid=5cc5c0f3d86170003e090a62&appid=afb5821958544dc9fedba14e4e46f849)

Actual Response (i.e.):  
{“dt”:1556452800,“t10”:284.6,“moisture”:0.146,“t0”:290.74}

---

<div class="post-metadata">

**Author:** ![Rocodamelshekima](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/rocodamelshekima/32/8209_2.png) [@Rocodamelshekima](https://community.homey.app/u/Rocodamelshekima)\
**Post date:** [April 29, 2019, 7:02pm UTC](https://community.homey.app/t/calling-an-external-service-and-receiving-the-response-in-a-variable/12445/2 "2019-04-29T19:02:40Z")

</div>

Maybe with [the HTTP app](https://apps.athom.com/app/com.internet) from @Erik ?

 ![chrome_2019-04-29_21-00-36](https://us1.discourse-cdn.com/flex025/uploads/athom/original/2X/c/c09a424871f91a8326ca3d695d426a4ad5289425.jpeg)

---

<div class="post-metadata">

**Author:** ![Jef\_van\_Leeuwen](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jef_van_leeuwen/32/9801_2.png) [@Jef\_van\_Leeuwen](https://community.homey.app/u/Jef_van_Leeuwen)\
**Post date:** [April 29, 2019, 9:37pm UTC](https://community.homey.app/t/calling-an-external-service-and-receiving-the-response-in-a-variable/12445/3 "2019-04-29T21:37:26Z")

</div>

Thx, but what to put where in the suggested Get card?  
Shout it be:

- url without parameters inurl (i.e. [http://api.agromonitoring.com/agro/1.0/soil](http://api.agromonitoring.com/agro/1.0/soil) )
- ?polyid=5cc…?appid=afb5… in JSONpath ( but then in JSON format… {“polyid”:"5cc… , “appid”:“afb5…”}
- variable: a Better Logig Variable (type string?) to hold the response?  
And in a separate flow breakdown of Response in different variables ?

This are some of the choices i consider te make, thats why i try to find some specialists for help…

---

<div class="post-metadata">

**Author:** ![Erik](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/erik/32/9865_2.png) [@Erik](https://community.homey.app/u/Erik)\
**Post date:** [April 30, 2019, 6:17pm UTC](https://community.homey.app/t/calling-an-external-service-and-receiving-the-response-in-a-variable/12445/4 "2019-04-30T18:17:18Z")

</div>

Replied via PM with working example 🙂

---

<div class="post-metadata">

**Author:** ![Rocodamelshekima](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/rocodamelshekima/32/8209_2.png) [@Rocodamelshekima](https://community.homey.app/u/Rocodamelshekima)\
**Post date:** [April 30, 2019, 6:44pm UTC](https://community.homey.app/t/calling-an-external-service-and-receiving-the-response-in-a-variable/12445/5 "2019-04-30T18:44:25Z")

</div>

Would u mind to put the working example also here for future reference?  
@Erik ^^

---

<div class="post-metadata">

**Author:** ![Jef\_van\_Leeuwen](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jef_van_leeuwen/32/9801_2.png) [@Jef\_van\_Leeuwen](https://community.homey.app/u/Jef_van_Leeuwen)\
**Post date:** [May 2, 2019, 9:46pm UTC](https://community.homey.app/t/calling-an-external-service-and-receiving-the-response-in-a-variable/12445/6 "2019-05-02T21:46:23Z")

</div>

Thx, i understand your solution but on my Homey i now have a problem to use any TAG,  
whenever i select one, Homey app aborts. Have to solve this first before i can implement your example.

---

<div class="post-metadata">

**Author:** ![Erik](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/erik/32/9865_2.png) [@Erik](https://community.homey.app/u/Erik)\
**Post date:** [May 3, 2019, 6:13am UTC](https://community.homey.app/t/calling-an-external-service-and-receiving-the-response-in-a-variable/12445/7 "2019-05-03T06:13:54Z")

</div>

Have you tried it with a browser via flow.homey.app?

---

<div class="post-metadata">

**Author:** ![Jef\_van\_Leeuwen](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jef_van_leeuwen/32/9801_2.png) [@Jef\_van\_Leeuwen](https://community.homey.app/u/Jef_van_Leeuwen)\
**Post date:** [May 3, 2019, 10:46am UTC](https://community.homey.app/t/calling-an-external-service-and-receiving-the-response-in-a-variable/12445/8 "2019-05-03T10:46:36Z")

</div>

Thx that is the solution, Safari IPhone 5 was the problem…  
Everything works now.

---

<div class="post-metadata">

**Author:** ![Jef\_van\_Leeuwen](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jef_van_leeuwen/32/9801_2.png) [@Jef\_van\_Leeuwen](https://community.homey.app/u/Jef_van_Leeuwen)\
**Post date:** [May 3, 2019, 10:58am UTC](https://community.homey.app/t/calling-an-external-service-and-receiving-the-response-in-a-variable/12445/9 "2019-05-03T10:58:04Z")

</div>

A Nice and relative simple solution for a quite common problem when calling external services that can be called with HTTP. Please ASK Erik to put iT as reply on this forum.  
By the was, one has to use the browser as Erik suggested.

---

<div class="post-metadata">

**Author:** ![Pino](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/pino/32/22822_2.png) [@Pino](https://community.homey.app/u/Pino)\
**Post date:** [May 3, 2019, 12:34pm UTC](https://community.homey.app/t/calling-an-external-service-and-receiving-the-response-in-a-variable/12445/10 "2019-05-03T12:34:11Z")

</div>

@Erik, @Arnfinn_Gilberg,

Hi Erik,

As suggested by Jef and previously asked by Roco. Can you please share the solution you sent to Jef? Both Arnfinn and myself are also looking for this solution.

---

<div class="post-metadata">

**Author:** ![Pino](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/pino/32/22822_2.png) [@Pino](https://community.homey.app/u/Pino)\
**Post date:** [May 3, 2019, 3:55pm UTC](https://community.homey.app/t/calling-an-external-service-and-receiving-the-response-in-a-variable/12445/11 "2019-05-03T15:55:22Z")

</div>

And scrolling by this topic again I noticed it was Jefs first post and i failed to welcome him to the Homey community and hope he’ll share and experience the fun and ease of Homey! Welcome Jef!

---

<div class="post-metadata">

**Author:** ![Rocodamelshekima](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/rocodamelshekima/32/8209_2.png) [@Rocodamelshekima](https://community.homey.app/u/Rocodamelshekima)\
**Post date:** [May 3, 2019, 5:32pm UTC](https://community.homey.app/t/calling-an-external-service-and-receiving-the-response-in-a-variable/12445/12 "2019-05-03T17:32:40Z")

</div>

Talked to Erik and when he finds some time he will post the solution here. 😉

---

<div class="post-metadata">

**Author:** ![Erik](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/erik/32/9865_2.png) [@Erik](https://community.homey.app/u/Erik)\
**Post date:** [May 4, 2019, 7:40am UTC](https://community.homey.app/t/calling-an-external-service-and-receiving-the-response-in-a-variable/12445/13 "2019-05-04T07:40:59Z")

</div>

Hi all,

Sorry for this delayed response, but I had to find some time for it.  
There are several ways to build the flows @Jef_van_Leeuwen requested.  
Because he wants to map multiple values from the same HTTP response it’s best to make 2 flows with combine cards _GET JSON trigger object (A24)_, _JSON object (T81)_ and _JSONpath Better Logic (A80)_.  
(Native Homey logic support is in development, but for now we rely on BetterLogic for storing variables).

When in doubt validate your JSON values for flow cards with [jsonlint.com](http://jsonlint.com/). Check your JSONpath expressions with [jsonpath.com](http://jsonpath.com/). If you’re using JSONpath expressions on XML responses, be aware of the XML to JSON conversion. This conversion can be simulated on [RunKit.com](https://runkit.com/585436e8fcbda700135741a7/586d421e08e18e001389a004) with the xml2js module.

The first flow will do a get request. The returned response will be available in the JSON token on the trigger of the second flow.  
In the second flow this JSON token can be used in the action cards, where the desired value is queried by JSONpath and put in Better Logic variables.

Preparation - Define Better Logic variables:  
 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/2X/a/ad81eaf607f3af80ebfac58e24a16a884032051f.png)

Flow 1 - Request (poll every hour):  
 ![2019-04-30%2020_05_06-Homey%20Flow](https://us1.discourse-cdn.com/flex025/uploads/athom/original/2X/e/e5d19a28286ffe0435b83b89570de937e7b4cf55.png)

Flow 2 - Extract values and map to Better Logic:  
Flow 1 triggers this flow because of the matching trigger-name ‘MonitorUpdate’

 ![2019-04-30%2020_06_03-Homey%20Flow](https://us1.discourse-cdn.com/flex025/uploads/athom/original/2X/5/567d32b2e9c0ceb26cf91878cf552da93a583fef.png)

---

<div class="post-metadata">

**Author:** ![Pino](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/pino/32/22822_2.png) [@Pino](https://community.homey.app/u/Pino)\
**Post date:** [May 4, 2019, 12:41pm UTC](https://community.homey.app/t/calling-an-external-service-and-receiving-the-response-in-a-variable/12445/14 "2019-05-04T12:41:05Z")

</div>

Hi @Erik,

Very much appreciated to share the example for all to learn and benefit!  
I will look into it and see if I can convert it to read an XML string from a local web server and send that to a variable! If anyone else has valid examples of how to work with the magnificent but in my opinion not simple 😉 HTTP request flow cards app please post these on this forum!

---

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [July 29, 2019, 12:24pm UTC](https://community.homey.app/t/calling-an-external-service-and-receiving-the-response-in-a-variable/12445/15 "2019-07-29T12:24:47Z")

</div>

Hi @Erik,  
The flows you published are very nice. I have about the same question, but…

I have a jsonfile where I get an hourly weather forecast. How can I get the data for example for 4 O’Clock instead off 3 O’Clock?

{“plaatsnaam”:[{“plaats”:“Utrecht”}],“data”:[{“tijd”:“1564405200”,“tijd\_nl”:“29-07-2019 15:00”,“offset”:“07”,“loc”:“none”,“temp”:“26”,“winds”:“2”,“windb”:“2”,“windknp”:“4”,“windkmh”:“7.2”,“windr”:“190”,“windrltr”:“Z”,“neersl”:“0”,“luchtd”:“1011”,“luchtdmmhg”:“758”,“luchtdinhg”:“29.84”,“hw”:“0”,“mw”:“0”,“lw”:“29”,“tw”:“29”,“rv”:“41”,“gr”:“155”,“cape”:“281.10”,“cond”:“3”,“ico”:“3”,“samenv”:“Halfbewolkt”,“icoon”:“halfbewolkt”},{“tijd”:“1564408800”,“tijd\_nl”:“29-07-2019 16:00”,“offset”:“08”,“loc”:“none”,“temp”:“27”,“winds”:“2”,“windb”:“2”,“windknp”:“4”,“windkmh”:“7.2”,“windr”:“191”,“windrltr”:“Z”,“neersl”:“0”,“luchtd”:“1011”,“luchtdmmhg”:“758.2”,“luchtdinhg”:“29.85”,“hw”:“0”,“mw”:“0”,“lw”:“35”,“tw”:“35”,“rv”:“39”,“gr”:“176”,“cape”:“281.29”,“cond”:“3”,“ico”:“3”,“samenv”:“Halfbewolkt”,“icoon”:“halfbewolkt”},

---

<div class="post-metadata">

**Author:** ![Erik](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/erik/32/9865_2.png) [@Erik](https://community.homey.app/u/Erik)\
**Post date:** [August 11, 2019, 3:18pm UTC](https://community.homey.app/t/calling-an-external-service-and-receiving-the-response-in-a-variable/12445/16 "2019-08-11T15:18:13Z")

</div>

Hi @Marcel_Ubels  
If the data is allways on the same place, lets say allways in the second element then `$.data[1].temp` will work fine.  
If the day and time is fixed then `$.data[?(@.tijd_nl=="29-07-2019 15:00")].temp` is a good query.  
Probably you want to get the data for a specific time, but ignore the day and month. That will work with this query: `$.data[?(@.tijd_nl.indexOf('15:00',0)>0)].temp`

---

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [August 11, 2019, 3:44pm UTC](https://community.homey.app/t/calling-an-external-service-and-receiving-the-response-in-a-variable/12445/17 "2019-08-11T15:44:18Z")

</div>

@Erik thx for your time. This way i will come quite a bit further. The first part I had, the second part with filtering I will try soon.

Data was always in the same place. So that worked fine

---

<div class="post-metadata">

**Author:** ![peltsi51](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/peltsi51/32/12564_2.png) [@peltsi51](https://community.homey.app/u/peltsi51)\
**Post date:** [September 21, 2019, 11:27am UTC](https://community.homey.app/t/calling-an-external-service-and-receiving-the-response-in-a-variable/12445/18 "2019-09-21T11:27:58Z")

</div>

Hi @Erik or someone else willing to help

Can you use the same method for getting information from “normal” web page. I’m trying to get the gasoline price data from webpage table. [www.polttoaine.net](http://www.polttoaine.net)

The simplified webpage xml code is following:

(“

1.570” is the value I want to get read to the variable) 

```
			<table id="Halvin_Kallein" style="width: 586px;">

				<tbody>
					<tr></tr>

					<tr class="bg2">

						<td></td>
						<td></td>
						<td></td>
						<td class="Hinnat">1.570
						</td>
					</tr>
				</tbody>
			</table>
		</div>
	</div>
</div>

```

So the JSONpath should be $.html.body.div[1].div[4].div[6].table.tbody.tr[1].td[2]

Is this right?

I used the following flow cards:  
For trigger:

 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/2X/1/159fbf9f49735dddc3b00cae70a451a45cd7296e.jpeg)

For working

 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/2X/7/76198e25a4bfe2ff6146a9a624ebddef538127cf.jpeg)

When I hit the test button, the trigger flow card shows that it is updated. The JSON data is saved in the strTest variable but the strTest2 variable isn’t updated.

I noticed that the strTest variable is containing the webpage xml code correctly. So the question is that can you use the GET requests in “ordinary” webpage to get the data? If you can, what whould I change in my flows to get it work?

---

<div class="post-metadata">

**Author:** ![Erik](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/erik/32/9865_2.png) [@Erik](https://community.homey.app/u/Erik)\
**Post date:** [September 21, 2019, 1:33pm UTC](https://community.homey.app/t/calling-an-external-service-and-receiving-the-response-in-a-variable/12445/19 "2019-09-21T13:33:16Z")

</div>

Hi!  
Unfortunantly HTML is in a lot cases not compliant with the XML convertor. For example when tags are not closed. Parsing this webpage results in this error:

`2019-09-21 15:30:52 [log] [HttpApp] 15:30:52 --> data is invalid XML (node:10017) UnhandledPromiseRejectionWarning: Error: Unexpected close tag Line: 48 Column: 7 Char: > at error (/node_modules/xml2js/node_modules/sax/lib/sax.js:667:10) at strictFail (/node_modules/xml2js/node_modules/sax/lib/sax.js:693:7) at closeTag (/node_modules/xml2js/node_modules/sax/lib/sax.js:887:9) at SAXParser.write (/node_modules/xml2js/node_modules/sax/lib/sax.js:1449:13) at Parser.exports.Parser.Parser.parseString (/node_modules/xml2js/lib/xml2js.js:503:31) at Parser.parseString (/node_modules/xml2js/lib/xml2js.js:7:59) at exports.parseString (/node_modules/xml2js/lib/xml2js.js:535:19) at Object.exports.convertToJson (/lib/util.js:12:7) at util.genericRequestHelper.then.result (/lib/flow/actions.js:110:24) at <anonymous>`

I don’t think you can workaround this issue with my app.

Erik

---

<div class="post-metadata">

**Author:** ![peltsi51](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/peltsi51/32/12564_2.png) [@peltsi51](https://community.homey.app/u/peltsi51)\
**Post date:** [September 21, 2019, 2:30pm UTC](https://community.homey.app/t/calling-an-external-service-and-receiving-the-response-in-a-variable/12445/20 "2019-09-21T14:30:30Z")

</div>

Hi.

Thanks for the fast answer and great app.

Might have to dive into Homey Script to have the solution for this problem.

[Next page](https://community.homey.app/t/calling-an-external-service-and-receiving-the-response-in-a-variable/12445.md?page=2)
