Supereg / homebridge-http-switch

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

"headers" parameter not working #56

Closed alessandrostuppino closed 4 years ago

alessandrostuppino commented 4 years ago

Describe the bug I'm trying to integrate homebridge-http-switch into my Homebridge system but when I try to add a device with it, including an header, it fails. I was trying to add a device with Authorization header in the following way:

{
    "accessory": "HTTP-SWITCH",
    "name": "Apple TV",
    "switchType": "toggle",
    "onUrl": {
        "url": "http://localhost:40304/Apple%20TV",
        "method": "POST",
        "body": "{ \"commands\": [ { \"key\": \"menu\", \"longPress\": false } ] }"
    },
    "offUrl": {
        "url": "http://localhost:40304/Apple%20TV",
        "method": "POST",
        "body": "{ \"commands\": [ { \"key\": \"home\", \"longPress\": true }, { \"key\": \"select\", \"longPress\": false } ] }"
    },
    "headers": {
        "Authorization": "TOKEN_APPLE_TV"
    }
}

Expected behavior When I try to use the device it is calling the URL but the status of the response is always 401 Unauthorized because it is not reading properly the header. I tried from Postman and through curl from console and it is working good.

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

Additional context I'm already using the plugin into the config, without headers, and it is working good.

mbierman commented 4 years ago

I think you have the headers in the wrong place.

D28CFDB7-45E0-40F6-A15F-8F809583932E

alessandrostuppino commented 4 years ago

Hi,

I also tried in that way but it is not working and in my opinion the readme is not really clear on where "headers" parameter should be. Then, instead of localhost there should be the IP of the raspberry where Homebridge is deployed. Do you think it could be a problem to do a POST to its own IP/localhost? because when I try to call the URL from my PC on postman it works properly while when I do the same through curl from Raspberry command line the call goes 200 OK but nothing happens

mbierman commented 4 years ago

Localhost means the raspberry pi itself. If you want to talk to an Apple TV you need it’s in address. You probably want to set it up with a fixed IP address via your router.

Also, I assume you inserted the token?

mbierman commented 4 years ago

Hi,

I also tried in that way but it is not working and in my opinion the readme is not really clear on where "headers" parameter should be. Then, instead of localhost there should be the IP of the raspberry where Homebridge is deployed. Do you think it could be a problem to do a POST to its own IP/localhost? because when I try to call the URL from my PC on postman it works properly while when I do the same through curl from Raspberry command line the call goes 200 OK but nothing happens

If everything works from the PC then it sounds like you have the Apple TV token etc correctly. Good start. Does the raspberry pi have full access to the same network?

I’m still, puzzled though about using localhost. Try two things:

  1. Put the header where I showed you. I agree the docs are confusing, but I’m sure what I posted works.
  2. Try changing localhost to the iPad of your Apple TV.
alessandrostuppino commented 4 years ago

Hi, I also tried in that way but it is not working and in my opinion the readme is not really clear on where "headers" parameter should be. Then, instead of localhost there should be the IP of the raspberry where Homebridge is deployed. Do you think it could be a problem to do a POST to its own IP/localhost? because when I try to call the URL from my PC on postman it works properly while when I do the same through curl from Raspberry command line the call goes 200 OK but nothing happens

If everything works from the PC then it sounds like you have the Apple TV token etc correctly. Good start. Does the raspberry pi have full access to the same network?

I’m still, puzzled though about using localhost. Try two things:

  1. Put the header where I showed you. I agree the docs are confusing, but I’m sure what I posted works.
  2. Try changing localhost to the iPad of your Apple TV.

Raspberry has same network of the PC. I'm using homebridge-apple-tv-remote plugin that allows me to create an API to control Apple TV through HTTP request. This plugin is not compatible with Alexa so I would like to use hombridge-http-switch to call the that APIs. BTW I placed the header where you said but still receiving Unauthorized and I tried to perform the call through cURL from Rasperry's command line and it is going in 200 OK but Apple TV is not turning on, but this is another problem 😋. I can't understand which would be the problem.

alessandrostuppino commented 4 years ago

It seems like I can’t make an HTTP Request from Raspberry to its own IP or something like that because, as I said, I tried to do it through cURL from command line, it returns me OK but Apple TV is not turning on. Really strange

mbierman commented 4 years ago

Is this the project you are using?

Instead of localhost try the ip of the raspberry pi.

I’m certain of the placement of the header for this plugin. The other one works differently.

But maybe I’m still confused. What are you doing with this plugin with regard to Apple TV?

Note the header has to be repeated in each command. In my example, only the last one had a header.

alessandrostuppino commented 4 years ago

Is this the project you are using?

Instead of localhost try the ip of the raspberry pi.

I’m certain of the placement of the header for this plugin. The other one works differently.

But maybe I’m still confused. What are you doing with this plugin with regard to Apple TV?

Note the header has to be repeated in each command. In my example, only the last one had a header.

Sorry for all these problems. I just tried to install another AppleTV Remote plugin and it is working properly. It seems that the issue is on the other side so I'm going to close the issue. Sorry if I wasted some of your time and thank you very much for the support.