peter-murray / node-hue-api

Node.js Library for interacting with the Philips Hue Bridge and Lights
Apache License 2.0
1.19k stars 145 forks source link

Api Error: body contains invalid json #31

Closed qkevinto closed 9 years ago

qkevinto commented 9 years ago

Thanks for sharing such a great project! It seems to be very well written and well documented.

However I'm having a bit of trouble running some basic functions such as registerUser() and setLightState(). It constantly throws a Api Error: body contains invalid json error. I have however been successful in running functions like connect() and deleteUser().

I'm running on a Windows 8.1 environment, with node version 0.10.35 and bridge swversion 01018228.

zenahirsch commented 9 years ago

I have also been running into this issue. My code is as follows:

var hue = require('node-hue-api'),
    HueApi = hue.HueApi,
    lightState = hue.lightState,
    elasticsearch = require('elasticsearch'),
    client = new elasticsearch.Client({ host: '[host here]' }),
    hostname = '[ip address here]',
    username = 'zenahirsch',
    api = new HueApi(hostname, username), 
    state = lightState.create();

var displayResult = function(result) {
    console.log(JSON.stringify(result, null, 2));
};

api.setLightState(1, state.off(), function(err, lights) {
    if (err) {
        console.log(err);
    }

    displayResult(lights);
});

I get the following result:

{ [Api Error: body contains invalid json]
  message: 'body contains invalid json',
  type: 2,
  address: '/lights/1/state' }
peter-murray commented 9 years ago

Can you check the version of request that is inside the request-util dependency of the hue-api?

I released an updated version of this support library to support the upcoming version 1.0.0 of the hue-api, and may have broken this. The new version of request changed the way the json property is interpreted and I suspect this may be your issue.

If confirmed, I can fix that quickly with a new patch release.

zenahirsch commented 9 years ago

package.json within request shows "version": "2.51.0"

peter-murray commented 9 years ago

Ok, I should be able to fix that quickly...

peter-murray commented 9 years ago

I have just released version 0.0.3 of request-util that should resolve this issue.

Run npm update on your project and it should pull this new version down which corrects the JSON issues.

Let me know if it works, and I can close out this issue.

zenahirsch commented 9 years ago

Yes, it worked! Thank you so much!

peter-murray commented 9 years ago

I am very sorry for that, I should have properly followed my semver version numbers and incremented it as a patch...