dxdc/homebridge-blinds

Empty body in request on POST

synowiec opened this issue · 3 comments

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

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"
    },
    ...
}

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

@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!