HTTP binding - Enhancing POST support

@J-N-K I have a device whose command / status interface is POST only. Here is my thing definition:

UID: http:url:e5a14ec3b0
label: Lareira - Comandos
thingTypeUID: http:url
configuration:
  authMode: BASIC
  ignoreSSLErrors: true
  password: *****
  baseURL: https://192.168.129.10:8000
  delay: 0
  refresh: 30
  commandMethod: POST
  contentType: text/plain
  timeout: 3000
  username: "*****"
  bufferSize: 2048
channels:
  - id: switch
    channelTypeUID: http:switch
    label: Switch
    description: ""
    configuration:
      onValue: "1"
      mode: WRITEONLY
      commandTransformation: JS:cmdEcoforestSwitch.js
      offValue: "0"
      commandExtension: recepcion_datos_4.cgi

And here is the transformation:

(function(i) {
    var resultado = "idOperacion=1013&on_off=" + i;
    return resultado.trim();
})(input)

This works Ok in OH3.0.0 with http binding updated to 3.1.0.snapshot. I am able to switch the device on and off. What I can’t do is to access the device status. For this I need to access the response to the POST request.

To obtain the status from the device I need to send a POST request with this body idOperacion=1002 and have access to the result, that comes like this (if I use CURL):

error_MODO_on_off=0
on_off=1
modo_operacion=1
modo_func=1
estado=20
consigna_potencia=5
consigna_temperatura=24.0
temperatura=23.6
temperatura_ext=---.-
0

Ideally this POST request should be sent periodically (equivalent to a status), but the HTTP binding only supports GET requests for status. Would it be possible to enhance it to allow POST requests for status ?

The question is how to configure the body. Do you have an idea? Maybe extend the YAML above to show what you think.

Thanks for your reply.

I think that there are two points to consider:

  1. pick the POST response. This would be a string that can be parsed by a transformation. Currently the binding does not allow to process a POST response
  2. In my own case all status requests should be handled using POST. But I can’t grant that this is a general case. Currently you have (in thing definitions) commandMethod: POST and contentType: text/plain. Adding statusMethod: POST and statuscontentType: text/plain would work in my case

Crosslink for github issue #9996

Here is what I suggest:

  1. In the thing definition I would add stateMethod with same options as commandMethod
  2. In the channel definition I would add stateBody as a fixed string (no parameters)
1 Like

Please check

update org.openhab.binding.http https://janessa.me/esh/org.openhab.binding.http-3.1.0-SNAPSHOT.jar
1 Like

Awesome. It works, although not exactly as I would like. The following section appears in Thing tab and it should be in the Channels tab instead because body content depends on the channel.

imagem

It works as it is now, but I need to define several things for the same device instead of several channels for the same thing, as I would like.

Please try again

Now it does not work at all, here is the log

2021-02-01 21:05:04.419 [WARN ] [ttp.internal.http.RefreshingUrlCache] - Creating request for 'idOperacion=1002$https://192.168.129.10:8000/recepcion_datos_4.cgi' failed: no protocol: idOperacion=1002$https://192.168.129.10:8000/recepcion_datos_4.cgi
2021-02-01 21:05:34.421 [WARN ] [ttp.internal.http.RefreshingUrlCache] - Creating request for 'idOperacion=1002$https://192.168.129.10:8000/recepcion_datos_4.cgi' failed: no protocol: idOperacion=1002$https://192.168.129.10:8000/recepcion_datos_4.cgi
2021-02-01 21:06:04.422 [WARN ] [ttp.internal.http.RefreshingUrlCache] - Creating request for 'idOperacion=1002$https://192.168.129.10:8000/recepcion_datos_4.cgi' failed: no protocol: idOperacion=1002$https://192.168.129.10:8000/recepcion_datos_4.cgi

Oops. Dumb mistake. I‘ll fix that tomorrow.

Thanks a lot. In the meantime I’ve seen your blog. Very nice family.

1 Like

Next try. I fixed the URL issue.

1 Like

Perfect. Fantastic. Thank you very much.