Supereg / homebridge-http-switch

Powerful http switch for Homebridge: https://github.com/homebridge/homebridge
ISC License
218 stars 36 forks source link

Request for Help with POST #115

Open peros550 opened 1 year ago

peros550 commented 1 year ago

Describe the bug This is a request for help rather than a bug report Expected behavior I have a working POST url that it control's Reolink camera's embeded siren.

To Reproduce Steps to reproduce the behavior: This is my working POST url. curl -X POST 'http://192.168.177.211/cgi-bin/api.cgi?user=admin&password=mypassword' -d '[{"cmd":"AudioAlarmPlay","action": 0, "param": {"alarm_mode": "times", "manual_switch": 1, "times": 3, "channel": 0}}]'

Version (output of npm list -g homebridge homebridge-http-switch)

Configuration This is my configuration so far:

{
    "accessory": "HTTP-SWITCH",
    "name": "Reolink Siren",
    "switchType": "stateless",
    "timeout": 3000,
    "debug": true,
    "onUrl": {
        "url": "http://192.168.177.211/cgi-bin/api.cgi?user=admin&password=mypassword",
        "method": "POST",
        "headers": {
            "Content-Type": "application/json"
        },
        "body": {
            "cmd": "AudioAlarmPlay",
            "action": 0,
            "param": {
                "alarm_mode": "times",
                "manual_switch": 1,
                "times": 3,
                "channel": 0
            }
        }
    }
}

Unfortunately, the plugin does not trigger the siren with the above configuration. Any ideas?
peros550 commented 1 year ago

Are there any issues with POST method? To the best of my knowledge (documentation seems a bit limited in regards to POST), it looks I have a correct configuration, yet the POST request is not issued.

Supereg commented 1 year ago

I See you have the debug variable set to true. Could you provide logs full logs of booting the plugin and turning on the switch?

pespinel commented 1 year ago

@Supereg Hi, I've been having problems with POST requests also.

I have this accessory:

{
    "accessory": "HTTP-SWITCH",
    "name": "Nuki Opener",
    "switchType": "stateless",
    "timeout": 1000,
    "debug": true,
    "onUrl": {
        "url": "https://api.nuki.io/smartlock/XXX/action",
        "method": "POST",
        "body": {
            "action": 3,
            "option": 0
        }
    },
    "headers": {
        "Content-Type": "application/json",
        "Authorization": "Bearer XXX"
    }
}

This is a POST request tested in Postman and working fine (I've removed sensitive information) but when I trigger the switch I get the following logs in homebridge:

[10/05/2023, 17:50:58] [Nuki Opener] setStatus() doing http request...
[10/05/2023, 17:50:59] [Nuki Opener] Error occurred setting state of switch: HTTP request returned with error code 401
[10/05/2023, 17:50:59] [Nuki Opener] Body of set response is: {"detailMessage":"Your access token is not authorized","stackTrace":[],"suppressedExceptions":[]}
[10/05/2023, 17:50:59] [Nuki Opener] Resetting switch to OFF

I know that It says that my token is not authorized but the same request in Postman works great with the same bearer token in the Authorization header

pespinel commented 1 year ago

@Supereg Hi, I've been having problems with POST requests also.

I have this accessory:

{
   "accessory": "HTTP-SWITCH",
   "name": "Nuki Opener",
   "switchType": "stateless",
   "timeout": 1000,
   "debug": true,
   "onUrl": {
       "url": "https://api.nuki.io/smartlock/XXX/action",
       "method": "POST",
       "body": {
           "action": 3,
           "option": 0
       }
   },
   "headers": {
       "Content-Type": "application/json",
       "Authorization": "Bearer XXX"
   }
}

This is a POST request tested in Postman and working fine (I've removed sensitive information) but when I trigger the switch I get the following logs in homebridge:

[10/05/2023, 17:50:58] [Nuki Opener] setStatus() doing http request...
[10/05/2023, 17:50:59] [Nuki Opener] Error occurred setting state of switch: HTTP request returned with error code 401
[10/05/2023, 17:50:59] [Nuki Opener] Body of set response is: {"detailMessage":"Your access token is not authorized","stackTrace":[],"suppressedExceptions":[]}
[10/05/2023, 17:50:59] [Nuki Opener] Resetting switch to OFF

I know that It says that my token is not authorized but the same request in Postman works great with the same bearer token in the Authorization header

@Supereg Checking the plugin initialization logs I think that the issue is that the headers are not being loaded correctly:

[10/05/2023, 18:39:33] [Nuki Opener] Initializing HTTP-SWITCH accessory...
[10/05/2023, 18:39:33] [Nuki Opener] Switch successfully configured...
[10/05/2023, 18:39:33] [Nuki Opener] Switch started with the following options: 
[10/05/2023, 18:39:33] [Nuki Opener]   - switchType: stateless
[10/05/2023, 18:39:33] [Nuki Opener]   - onUrls: [{"method":"POST","body":"{\"action\":3,\"option\":0}","repeat":1,"delayBeforeExecution":0,"auth":{"sendImmediately":true},"headers":{},"strictSSL":false,"requestTimeout":20000,"url":"https://api.nuki.io/smartlock/XXX/action"}]
[10/05/2023, 18:39:33] [Nuki Opener]   - timeout for stateless switch: 1000
kgrellus commented 1 year ago

Hey @pespinel,

your configuration looks faulty to me, the "headers" config property is a property of UrlObjects, try moving it into the "onUrl" config property:

"onUrl": {
  "url": "https://api.nuki.io/smartlock/XXX/action",
  "method": "POST",
  "headers": {
    "Content-Type": "application/json",
    "Authorization": "Bearer XXX"
  }
},