2

SO I've searched around and even asked on the neigh dead official forums. I've searched here but the responses are years old and oft contain dead links.

I'm simply trying to toggle a light in my house. I've auth'd via the debug tool already and gotten my "username" and then hardcoded it into this app. Again this is just me testing it. I've even copied a functional url (http://192.168.0.100/api/RjplsYoXQvdTl11DOVIo92SKNB7vYRfwZvqCzvDK/lights/2/) into other browsers and devices to ensure that i don't have to process through a reauth on different devices. Yes also I know i'm moving from sync to async but unless that's the problem i'm not worried about hanging the program there. I'm just trying to toggle something in the API :)

So the problem is the response is just a generic HTTP 200 OK response and not an API response as expected.

I get:

StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.HttpConnection+HttpConnectionResponseContent, Headers:
 {
 Server: nginx
 Date: Sun, 23 Sep 2018 18:37:44 GMT
 Connection: close
 Cache-Control: no-store, must-revalidate, no-cache, post-check=0, pre-check=0
 Pragma: no-cache
 Access-Control-Max-Age: 3600
 Access-Control-Allow-Origin: *
 Access-Control-Allow-Credentials: true
 Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE, HEAD
 Access-Control-Allow-Headers: Content-Type
 Content-Type: application/json
 Expires: Mon, 01 Aug 2011 09:00:00 GMT
 }

when i expect

   {"success":{"/lights/1/state/on":false}},

Here's the code. Can someone shine some light on this? Thanks

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json;


namespace ConsoleApp1
{

    class Program
    {
        static HttpClient client = new HttpClient();
        static HttpResponseMessage response = new HttpResponseMessage();

        public class StateO
        {
            public bool On { get; set; }
            public int Bri { get; set; }
        }
        public class Light
        {
            public string Name { get; set; }
            public StateO State { get; set; }
            public Light()
            {
                State = new StateO();
            }
        }

        static void Main(string[] args)
        {
            client.BaseAddress = new Uri("http://192.168.0.100/");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));
            UpdateProductAsync().GetAwaiter().GetResult();

        }
        public static async Task<Light> UpdateProductAsync()
        {
            Light light = new Light();
            light.State.On = false;
            string json = JsonConvert.SerializeObject(light);

            response = await client.PutAsJsonAsync(
                $"api/RjplsYoXQvdTl11DOVIo92SKNB7vYRfwZvqCzvDK/lights/2/", json);
            Console.WriteLine("potato: " + response.ToString());
            response.EnsureSuccessStatusCode();
            // light = await response.Content.ReadAsAsync<Light>();
            return light;
        }
    }

}
Xellinus
  • 33
  • 8
  • This whole thing was just some crazy dumb invalid json that I never really figured out here. I ran the same attempt through python and had to use json module to just json.dump the data. It works there. This whole thing is silly please move along and look for help from somewhere else :) hehe – Xellinus Nov 24 '19 at 03:26

1 Answers1

1

Is that IP address the correct one for your bridge? Your response header is coming from an Nginx server.

Also seems like you are a little confused with the Hue API endpoints.

PUT request to /api/<username>/lights/<id> is to rename lights

PUT request to /api/<username>/lights/<id>/state is to change light state

Documentation is here

miknik
  • 5,748
  • 1
  • 10
  • 26
  • Thanks. I didn't notice that little ID goof :) But yes it's the correct IP. Just checked it again. And I have the Documentation up. Unfortunately it's not helping me understand why this isn't working :( – Xellinus Sep 24 '18 at 00:06
  • Are you using Nginx as a reverse proxy between your machine and the hue bridge? – miknik Sep 24 '18 at 00:11
  • I do not believe so. As I've never intentionally set one up. :) Now this is even more weird. Navigating to the IP of my bridge gives a little Open Source blurb and lists a load of libs, frameworks, etc. and NGINX is listed there. That's all I know about that so far. – Xellinus Sep 24 '18 at 00:17
  • You should be able to confirm your bridge ip by visiting https://discovery.meethue.com/ – miknik Sep 24 '18 at 00:21
  • I have and recieved the response: [{"id":"001788fffe40f461","internalipaddress":"192.168.0.100"}]. – Xellinus Sep 24 '18 at 00:26
  • What do you get if you try this in your browser? http://192.168.0.100/api/RjplsYoXQvdTl11DOVIo92SKNB7vYRfwZvqCzvDK/lights – miknik Sep 24 '18 at 00:32
  • returns the full 'get' response. Lots of JSON. Here's a pastebin to show: https://pastebin.com/02Hwrk0n – Xellinus Sep 24 '18 at 00:36
  • Looks good, what happens when you run this? `curl -d '{"on":true}' -H "Content-Type: application/json" -X PUT http://192.168.0.100/api/RjplsYoXQvdTl11DOVIo92SKNB7vYRfwZvqCzvDK/lights/1/state` – miknik Sep 24 '18 at 00:52
  • [{"error":{"type":2,"address":"/lights/1/state","description":"body contains invalid json"}}] – Xellinus Sep 24 '18 at 00:54
  • Try typing this bit out, sometimes the quotes get screwy with copy/paste `'{"on":true}'` You are getting the right response from the bridge, it's just not happy with the formatting – miknik Sep 24 '18 at 01:02
  • Oh interesting. It hangs to think for a second. I get my hopes up and then "curl: (6) could not resolve host: application" Then i start crying all over again :) (BTW thank you for all this help. I greatly appreciate it!) And i can't get it to do it again. now it's just invalid json again? grrr – Xellinus Sep 24 '18 at 01:06
  • I must be having some stupid typo issues so i made a batch file to run it instead and it gives the invalid json error. – Xellinus Sep 24 '18 at 01:15
  • Weird, works fine for me. How about without any single quotes, like this? `curl -d "{\"on\":true}"` Also works fine for me – miknik Sep 24 '18 at 01:28
  • Welp that worked! However I feel we might have gotten slightly off center here. The code still returns HTTP Code 200 instead of anything from the bridge :) – Xellinus Sep 24 '18 at 01:41