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;
}
}
}