Can't get HTTP binding to work with Morph.io API

Hi,

I’m trying to get the HTTP Binding to work with my custom API, but it just won’t work.

Here’s a sample from my API:

[
  {"price_yesterday":"0.5419","name":"Burgenland","price_today":"0.5419","price_difference":"0.0","id":"burgenland"},
  {"price_yesterday":"0.5459","name":"Kärnten","price_today":"0.5459","price_difference":"0.0","id":"kaernten"},
  {"price_yesterday":"0.5529","name":"Niederösterreich","price_today":"0.5529","price_difference":"0.0","id":"niederoesterreich"},
  {"price_yesterday":"0.5172","name":"Oberösterreich","price_today":"0.5172","price_difference":"0.0","id":"oberoesterreich"},
  {"price_yesterday":"0.54","name":"Salzburg","price_today":"0.54","price_difference":"0.0","id":"salzburg"},
  {"price_yesterday":"0.5313","name":"Steiermark","price_today":"0.5313","price_difference":"0.0","id":"steiermark"},
  {
    "price_yesterday":  "0.582",
    "name":             "Tirol",
    "price_today":      "0.582",
    "price_difference": "0.0",
    "id":               "tirol"
  },
  {"price_yesterday":"0.582","name":"Vorarlberg","price_today":"0.582","price_difference":"0.0","id":"vorarlberg"},
  {"price_yesterday":"0.5579","name":"Wien","price_today":"0.5579","price_difference":"0.0","id":"wien"}
]

Here’s the item definition and the transformation file:

Number Heizung_Oelpreis "Ölpreis [%.3f €/l]" <oil> {http="<[https://api.morph.io/reitermarkus/heizoelpreise-oesterreich/data.json?key=XXXXXXXXXXXXXXXXXXXX&query=select%20%2A%20from%20%27data%27:10000:JS(Heizung_Oelpreis.js)]"}

(function(i) {

  var results = JSON.parse(i),
      i       = results.length

  while (--i && results[i]["id"] != "tirol") {}

  return results[i]["price_today"]

})(input)

It’s throwing this error:

2016-02-17 07:18:43.446 [ERROR] [.service.AbstractActiveService] - Error while executing background thread HTTP Refresh Service
java.util.UnknownFormatConversionException: Conversion = '2'
	at java.util.Formatter.checkText(Formatter.java:2579) ~[na:1.8.0_66]
	at java.util.Formatter.parse(Formatter.java:2565) ~[na:1.8.0_66]
	at java.util.Formatter.format(Formatter.java:2501) ~[na:1.8.0_66]
	at java.util.Formatter.format(Formatter.java:2455) ~[na:1.8.0_66]
	at java.lang.String.format(String.java:2940) ~[na:1.8.0_66]
	at org.openhab.binding.http.internal.HttpBinding.execute(HttpBinding.java:139) ~[na:na]
	at org.openhab.core.binding.AbstractActiveBinding$BindingActiveService.execute(AbstractActiveBinding.java:156) ~[na:na]
	at org.openhab.core.service.AbstractActiveService$RefreshThread.run(AbstractActiveService.java:173) ~[na:na]

Have you tried using JSONPATH instead of JS?

I have just learned about JSONPATH today.

The JSONPATH syntax seems to be right but it’s still showing the same error, so the transformation doesn’t seem to be the issue.

Number Heizung_Oelpreis "Ölpreis [%.3f €/l]" <oil> {http="<[https://api.morph.io/reitermarkus/heizoelpreise-oesterreich/data.json?key=XXXXXXXXXXXXXXXXXXXX&query=select%20%2A%20from%20%27data%27:20000:JSONPATH($[?(@.id=='tirol')].price_today)]"}

As a first step, you could try JSONPATH with a string Item, so you can see, what’s the result of the transformation.

Ok, I finally got it working.

First of all, I had to use an external shell script with curl to even get a response, the HTTP binding wouldn’t work, neither would an inline EXEC curl.

The second problem was that my API was showing the numbers as strings, which I fixed.

Lastly, the JSONPATH above returned an array, which is strange, but I simple added a [0] to retrieve the value:

JSONPATH($[?(@.id=='tirol')].price_today[0])

But does anyone know why neither the HTTP nor the EXEC binding are able to fetch an URL which is perfectly valid like this:

https://api.morph.io/reitermarkus/heizoelpreise-oesterreich/data.json?key=XXXXXXXXXXXXXXXXXXXX&query=select%20%2A%20from%20%27data%27

Is there anything in this URL that has to be escaped?

Ok, so I tried setting this up as a cached item in the http binding like this:

http:oilprice.url=https://api.morph.io/reitermarkus/heizoelpreise-oesterreich/data.json?key=XXXXXXXXXXXXXXXXXXXX&query=select%20%2A%20from%20%27data%27
http:oilprice.updateInterval=30000

And it throws this error:

2016-02-21 18:18:42.158 [ERROR] [g.openhab.io.net.http.HttpUtil] - Fatal transport error: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
2016-02-21 18:18:42.158 [ERROR] [.o.b.http.internal.HttpBinding] - No response received from 'oilprice'

So it seems to be an SSL error, what could the cause of this be?

Java and api.morph.io may not share a common certificate authority:

These instructions might help in this situation.

Thank you, this fixed it!

I’ll leave this here for future reference (note: /usr/libexec/java_home is OS X specific):

#!/usr/bin/env sh

export JAVA_HOME="$(/usr/libexec/java_home)"

CERT="/tmp/startcom-ca.pem"

curl -sL "https://www.startssl.com/certs/ca.pem" -o "${CERT}"

"${JAVA_HOME}/bin/keytool" -import -trustcacerts -noprompt -keystore "${JAVA_HOME}/jre/lib/security/cacerts" \
  -alias StartCom-Root-CA -storepass changeit -file "${CERT}"
1 Like