dxdc / homebridge-blinds

:sunrise: Homebridge Plugin to control my blinds over HTTP
https://www.npmjs.com/package/homebridge-blinds
ISC License
54 stars 25 forks source link

Empty body in request on POST #49

Closed synowiec closed 3 years ago

synowiec commented 3 years ago

Hey,

Seems that body content in POST is not working properly; seems that body even though is filled seems empty: This is my config:

{ "name": "Blind", "up_url": { "url": "http://192.168.1.5/msg.htm", "method": "POST", "body": "send_ch=4&send_act=160" }, "down_url": { "url": "http://192.168.1.5/msg.htm", "method": "POST", "body": "send_ch=4&send_act=192" }, "stop_url": { "url": "http://192.168.1.5/msg.htm", "method": "POST", "body": "send_ch=4&send_act=144" }, "verbose": true, "accessory": "BlindsHTTP" }

And triggering actions returns UNKNOWN_STATE on blinds, which means body was empty.

Simple curl:

curl -X POST -d "send_ch=4&send_act=160" http://192.168.1.5/msg.htm

Is working like charm.

Any ideas why body is empty?

Cheers! PS

dxdc commented 3 years ago

I think there is some confusion on how to properly run a POST request.

The -d flag on curl means it is sending url-encoded form data.

If you review the Forms section of the request library, it will show you some examples of how to accomplish this. There is also a helpful utility that can help you with this as well, although it doesn't appear to work for posting form data.

Please revise your configuration, like this, and try again:

{
    "name": "Blind",
    "up_url": {
        "url": "http://192.168.1.5/msg.htm",
        "method": "POST",
        "form": {
            "send_ch": 4,
            "send_act": 160
        }
    },
    "down_url": {
        "url": "http://192.168.1.5/msg.htm",
        "method": "POST",
        "form": {
            "send_ch": 4,
            "send_act": 192
        }
    },
    "stop_url": {
        "url": "http://192.168.1.5/msg.htm",
        "method": "POST",
        "form": {
            "send_ch": 4,
            "send_act": 144
        }
    },
    ...
}

This version should also work, if you prefer. I have a feeling that because application/x-www-form-urlencoded is not being set (as in your previous example), body is not being interpreted properly. At any rate, the form approach is simpler since those aspects are handled by request.

{
    "name": "Blind",
    "up_url": {
        "url": "http://192.168.1.5/msg.htm",
        "method": "POST",
        "headers": {
            "Content-Type": "application/x-www-form-urlencoded",
            "Content-Length": 22
        },
        "body": "send_ch=4&send_act=160"
    },
    "down_url": {
        "url": "http://192.168.1.5/msg.htm",
        "method": "POST",
        "headers": {
            "Content-Type": "application/x-www-form-urlencoded",
            "Content-Length": 22
        },
        "body": "send_ch=4&send_act=192"
    },
    "stop_url": {
        "url": "http://192.168.1.5/msg.htm",
        "method": "POST",
        "headers": {
            "Content-Type": "application/x-www-form-urlencoded",
            "Content-Length": 22
        },
        "body": "send_ch=4&send_act=144"
    },
    ...
}
synowiec commented 3 years ago

I've tested both of your resolution and as first one doesn't work - second one works like charm. I've already tried both of them before, but seems that "Content-Length": 22 was missing and without it calls won't work.

Anyway issue is resolved, thank you very much!

dxdc commented 3 years ago

@synowiec

"Content-Length": 22 was missing and without it calls won't work.

Some web servers are picker about this than others. Glad it's working for you!