How to send header via HTTP binding?

Hi.
I need to get weather data from Yandex (50 free requests per day allowed).
Here is api:

GET https://api.weather.yandex.ru/v2/informers?
 lat=<lat>
 & lon=<lon>
 & [lang=<lang>]

X-Yandex-API-Key: <key>

How to send header “X-Yandex-API-Key:xxxxxxxxxxxx” via HTTP binding?

Working curl:

curl -H "X-Yandex-API-Key:xxxxxxxxxxxx" -G "https://api.weather.yandex.ru/v2/informers?lat=43.59144&lon=39.72804&lang=en_US"

What exactly is unclear in the docs: HTTP - Bindings | openHAB?

If i use:

Thing http:url:yandexWeather "yandexWeather" [
	baseURL="https://api.weather.yandex.ru/v2/informers?lat=43.59144&lon=39.72804&lang=en_US", 
	headers="X-Yandex-API-Key=xxxxxxxxxxxxxxx",
	refresh=60]

Got error:

Requesting 'https://api.weather.yandex.ru/v2/informers?lat=43.59144&lon=39.72804&lang=en_US' (method='GET', content='null') failed: 403 Forbidden

If i use rules - works ok:

val headers = newHashMap("X-Yandex-API-Key" -> "xxxxxxxxxxxxx")
val output = sendHttpGetRequest("https://api.weather.yandex.ru/v2/informers?lat=43.59144&lon=39.72804&lang=en_US", headers, 1000)

{"now":1631361158,"now_dt":"2021-09-11T11:52:38.214470Z","info":{"url":"https://yandex.ru/pogoda/239?lat=43.59144\u0026lon=39.72804","lat":43.59144,"lon":39.72804},"fact":{"obs_time":1631358000,"temp":20,"feels_like":22,"temp_water":25,"icon":"ovc_ra","condition":"rain","wind_speed":1.5,"wind_dir":"nw","pressure_mm":755,"pressure_pa":1006,"humidity":87,"daytime":"d","polar":false,"season":"autumn","wind_gust":5.4},"forecast":{"date":"2021-09-11","date_ts":1631307600,"week":36,"sunrise":"05:55","sunset":"18:39","moon_code":10,"moon_text":"moon-code-10","parts":[{"part_name":"evening","temp_min":19,"temp_avg":19,"temp_max":20,"temp_water":25,"wind_speed":1.5,"wind_gust":6.2,"wind_dir":"se","pressure_mm":755,"pressure_pa":1006,"humidity":88,"prec_mm":2.4,"prec_prob":80,"prec_period":360,"icon":"ovc_-ra","condition":"light-rain","feels_like":20,"daytime":"n","polar":false},{"part_name":"night","temp_min":19,"temp_avg":19,"temp_max":20,"temp_water":25,"wind_speed":1.8,"wind_gust":6.1,"wind_dir":"e","pressure_mm":755,"pressure_pa":1006,"humidity":84,"prec_mm":3.3,"prec_prob":60,"prec_period":360,"icon":"ovc_ra","condition":"rain","feels_like":20,"daytime":"n","polar":false}]}}

Binding version?

265 │ Active │ 80 │ 3.2.0.M1 │ openHAB Add-ons :: Bundles :: HTTP Binding

Please enable TRACE logging and show what is actually send. Maybe an encoding problem in the API key?


2021-09-11 15:31:27.291 [WARN ] [re.config.core.internal.ConfigMapper] - Could not set field value for field 'headers': Can not set java.util.ArrayList field org.openhab.binding.http.internal.config.HttpThingConfig.headers to java.util.Im
mutableCollections$List12
java.lang.IllegalArgumentException: Can not set java.util.ArrayList field org.openhab.binding.http.internal.config.HttpThingConfig.headers to java.util.ImmutableCollections$List12
        at jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167) ~[?:?]
        at jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171) ~[?:?]
        at jdk.internal.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81) ~[?:?]
        at java.lang.reflect.Field.set(Field.java:780) ~[?:?]
        at org.openhab.core.config.core.internal.ConfigMapper.as(ConfigMapper.java:101) [bundleFile:?]
        at org.openhab.core.config.core.Configuration.as(Configuration.java:80) [bundleFile:?]
        at org.openhab.core.thing.binding.BaseThingHandler.getConfigAs(BaseThingHandler.java:218) [bundleFile:?]
        at org.openhab.binding.http.internal.HttpThingHandler.initialize(HttpThingHandler.java:139) [bundleFile:?]
        at org.openhab.core.thing.binding.BaseThingHandler.thingUpdated(BaseThingHandler.java:152) [bundleFile:?]
        at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
        at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:?]
        at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
        at java.lang.reflect.Method.invoke(Method.java:566) ~[?:?]
        at org.openhab.core.internal.common.AbstractInvocationHandler.invokeDirect(AbstractInvocationHandler.java:154) [bundleFile:?]
        at org.openhab.core.internal.common.Invocation.call(Invocation.java:52) [bundleFile:?]
        at java.util.concurrent.FutureTask.run(FutureTask.java:264) [?:?]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) [?:?]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) [?:?]
        at java.lang.Thread.run(Thread.java:829) [?:?]
2021-09-11 15:31:27.291 [DEBUG] [org.eclipse.jetty.server.HttpOutput ] - write(array) s=OPEN,api=BLOCKING,sc=false,e=null aggregated !flush DirectByteBuffer@2a4473e9[p=0,l=14,c=32768,r=14]={<<<event: message>>>\ndata: {"...t:8080/}
2021-09-11 15:31:27.291 [DEBUG] [org.eclipse.jetty.server.HttpOutput ] - write(array HeapByteBuffer@29000e5e[p=0,l=1,c=1,r=1]={<<<\n>>>})
2021-09-11 15:31:27.291 [INFO ] [nding.http.internal.HttpThingHandler] - Using the secure client for thing 'http:url:yandexWeather'.
2021-09-11 15:31:27.291 [DEBUG] [org.eclipse.jetty.server.HttpOutput ] - write(array) s=OPEN,api=BLOCKING,sc=false,e=null aggregated !flush DirectByteBuffer@2a4473e9[p=0,l=15,c=32768,r=15]={<<<event: message\n>>>data: {"t...t:8080/}
2021-09-11 15:31:27.292 [DEBUG] [org.eclipse.jetty.server.HttpOutput ] - write(array HeapByteBuffer@6fc93b66[p=0,l=6,c=6,r=6]={<<<data: >>>})
2021-09-11 15:31:27.292 [DEBUG] [org.eclipse.jetty.server.HttpOutput ] - write(array) s=OPEN,api=BLOCKING,sc=false,e=null aggregated !flush DirectByteBuffer@2a4473e9[p=0,l=21,c=32768,r=21]={<<<event: message\ndata: >>>{"topic":...t:8080/}
2021-09-11 15:31:27.292 [DEBUG] [org.eclipse.jetty.server.HttpOutput ] - write(array HeapByteBuffer@473d3439[p=0,l=123,c=123,r=123]={<<<{"topic":"openhab/items/d...type":"ItemStateEvent"}>>>})
2021-09-11 15:31:27.292 [DEBUG] [org.eclipse.jetty.server.HttpOutput ] - write(array) s=OPEN,api=BLOCKING,sc=false,e=null aggregated !flush DirectByteBuffer@2a4473e9[p=0,l=144,c=32768,r=144]={<<<event: message\ndata: {"to...type":"ItemSta
teEvent"}>>>vent"}:0C...t:8080/}
2021-09-11 15:31:27.293 [DEBUG] [org.eclipse.jetty.server.HttpChannel] - sendResponse info=null content=DirectByteBuffer@2a4473e9[p=0,l=144,c=32768,r=144]={<<<event: message\ndata: {"to...type":"ItemStateEvent"}>>>vent"}:0C...t:8080/} com
plete=false committing=false callback=Blocker@62a10141{null}
2021-09-11 15:31:27.293 [DEBUG] [.eclipse.jetty.server.HttpConnection] - generate: FLUSH for SendCallback@3edb2f1[PROCESSING][i=null,cb=org.eclipse.jetty.server.HttpChannel$SendCallback@fc1cd96] (null,[p=0,l=144,c=32768,r=144],false)@COMM
ITTED


So the issue is the configuration. Can you try setting that as a managed thing (via UI) and see if that works?

Yes, via UI it works.

But where is mistake via Files config?

Don‘t know, I never use files (exactly because of the issues you encounter now). Looks like a documentation issue, though. I doubt it‘s a bug in the Config file parsing (and if that would be the case, it‘s a core issue, not an addon issue).

2 Likes

Have you tried to change the = to : in the header field configuration? If I remember correctly header key and value are separated by colon.

I guess the issue is that in the text file the value is considered as String, not as list of String and therefore the config Mapping fails. But I have no idea how to change that.

Yes, error:

021-09-12 09:21:36.857 [WARN ] [re.config.core.internal.ConfigMapper] - Could not set field value for field 'headers': Can not set java.util.ArrayList field org.openhab.binding.http.internal.config.HttpThingConfig.headers to java.util.ImmutableCollections$List12
java.lang.IllegalArgumentException: Can not set java.util.ArrayList field org.openhab.binding.http.internal.config.HttpThingConfig.headers to java.util.ImmutableCollections$List12

Hello,

Thanks for update and quick reply, its work for me, Really helpful and thankful.

I was able to solve the string-array conversion by putting an empty string after the key.

headers="X-Yandex-API-Key=xxxxxxxxxxxxxxx", "",