3

I started playing around with the Philips Hue Bridge API by sending it requests using Postman. I was able to authenticate myself, create a user and even successfully turn lights on or off. Conclusion: I am able to reach the API.

Next I made a mini test javascript file to do the exact same using Node.js and axios@0.21.4. When I try the following code everything works and I receive the expected JSON responses:

const axios = require('axios')

axios({
    method: 'GET',
    url: 'http://philips-hue.local/api/BYLCIlxABzz2eDHAxx70T'
})
    .then(({data}) => {
        console.log('------> AXIOS RES: ', data)
    })
    .catch((err) => {
        console.log('------> AXIOS ERR: ', err)
    })

Conclusion: Axios is able to communicate with the API.

Next I wanted to build this into an Express.js endpoint. However when I try to use the same code inside of express@4.17.1 I get an error:

router.route('/getlights').get((req, res) => {
    axios({
        method: 'GET',
        url: 'http://philips-hue.local/api/BYLCIlxABzz2eDHAxx70T/lights'
        })
        .then(({data}) => {
            console.log('------> AXIOS RES: ', data)
            res.json({
                state: 'SUCCESS',
                data
            })
        })
        .catch((err) => {
            console.log('------> AXIOS ERR: ', err)
            res.json({
                state: 'ERROR',
                err
            })
        })
})

Response:

{
    "state": "ERROR",
    "err": {
        "message": "connect EHOSTUNREACH 192.168.0.107:80",
        "name": "Error",
        "stack": "Error: connect EHOSTUNREACH 192.168.0.107:80\n    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1161:16)",
        "config": {
            "url": "http://philips-hue.local/api/BYLCIlxABzz2eDHAxx70T/lights",
            "method": "get",
            "headers": {
                "Accept": "application/json, text/plain, */*",
                "User-Agent": "axios/0.21.1"
            },
            "transformRequest": [
                null
            ],
            "transformResponse": [
                null
            ],
            "timeout": 0,
            "xsrfCookieName": "XSRF-TOKEN",
            "xsrfHeaderName": "X-XSRF-TOKEN",
            "maxContentLength": -1,
            "maxBodyLength": -1
        },
        "code": "EHOSTUNREACH"
    }
}

I started troubleshooting the problem by first trying to changing the URL to a random online place holder like http://jsonplaceholder.typicode.com/posts/1 just to see if I could establish any connection at all. This actually worked right away.

This is where I got really confused. Why am I able to successfully communicate with the bridge using Postman or Axios in a javascript file, but NOT able to connect with it when the same Axios code is used by express. The problems seems to occur only when using both axios and express to connect to the bridge API.

I have already tried retesting this with node-fetch and 3 other versions of Axios. All tests failed the exact same way. The code by itself can always connect to the API but once the code is being called by express it causes the EHOSTUNREACH error.

I ran out of ideas on how to further google this issue since I am not sure what exactly is causing the problem (express and/or axios and/or bridge). Any trouble shooting ideas or google keywords are also very welcome. :)

Philipp Panik
  • 254
  • 1
  • 4
  • 15

2 Answers2

0

I had the same error, for some reason the ip address of my hue bridge had changed so I went into the hue app on my smartphone and copied the new ip address. IDK if this is helpfull, im pretty new here.

  • 1
    Hi, welcome to the community! The IP is the same for me always. I can see this in the response as well as test it with other methods. I think the problem is a little deeper. :/ – Philipp Panik Feb 21 '22 at 15:11
0

I had the same error since yesterday, and couldn't find solution. What actually caused mine was, there was a bearer token on the 3rd party api I called and i also pass it through the header as well.

So it was removed and pass the token through header and it works fine.

UPDATE: error keeps showing up if send another request. The solution i found was to use request.

const request = require('request');
const headers = {
    'Content-Type': 'application/json',
    'token': `${token}`
};
const data = {
       username : 'username'
}
let options = {
       method: 'POST',
       url: 'url',
       port: 443,
       headers: headers,
       body: data,
       json: true
};

request(options, async (error, res, body) => {
       if (error) {
           console.log({error});
           return response.status(500).json({
               status: false,
               message: "Error occured"
           });
       } else {
           console.log({body});
           return response.status(201).json({
               message: "success"
           });
       }
});
Ibrahim Hammed
  • 833
  • 1
  • 8
  • 17