I want to turn on a light by sending a POST with my “httpClientLight” method to my home assistant’s API locally on my network.
I’m using the httpclient to try to send the post request (code below), but I keep getting 400 errors.
Attempts:
-
I know the API call is making it to home assistant (see the packet capture below)
-
I’ve tried the Content-Length fix ({ “Content-Length” , “42” },) but then I get a content-length error message in the particle logs.
-
I know I could use MQTT but that seems really complicated for something I can achieve with a curl command, or in this case httpclient.
Any thoughts?
#include <HttpClient.h>
http_header_t headers[] = {
{ "Accept" , "*/*"},
{ "Authorization" , "Bearer <redacted>"},
{ "Content-Type", "application/json" },
//{ "Content-Length" , "42" },
{ NULL, NULL } // NOTE: Always terminate headers will NULL
};
HttpClient http;
http_request_t request;
http_response_t response;
void httpClientLight() {
request.ip = IPAddress(172, 16, 66, 251);
request.port = 8123;
request.path = "/api/services/switch/turn_on";
request.body = '{"entity_id": "garage_light"}' ;
http.post(request, response, headers)
}
Packet capture of what home assistant it seeing:
0x0000: 4500 0181 0036 0000 fe06 e81f ac10 426d E....6........Bm
0x0010: c0a8 23fb fa12 1fbb 0000 19d6 3141 6086 ..#.........1A`.
0x0020: 5018 1200 e2d1 0000 202f 6170 692f 7365 P......../api/se
0x0030: 7276 6963 6573 2f73 7769 7463 682f 7475 rvices/switch/tu
0x0040: 726e 5f6f 6e20 4854 5450 2f31 2e31 0d0a rn_on.HTTP/1.1..
0x0050: 436f 6e6e 6563 7469 6f6e 3a20 636c 6f73 **Connection:.clos**
0x0060: 650d 0a43 6f6e 7465 6e74 2d4c 656e 6774 e..Content-Lengt
0x0070: 683a 2031 300d 0a41 6363 6570 743a 202a h:.10..Accept:.*
0x0080: 2f2a 0d0a 4175 7468 6f72 697a 6174 696f /*..Authorizatio
0x0090: 6e3a 2042 6561 7265 7220 6579 4a30 6558 n:.Bearer.eyJ0eX
0x00a0: 4169 4f69 4a4b 5631 5169 4c43 4a68 6247 AiOiJKV1QiLCJhbG
0x00b0: 6369 4f69 4a49 557a 4931 4e69 4a39 2e65 ciOiJIUzI1NiJ9.e
0x00c0: 794a 7063 334d 694f 6949 305a 6a55 7859 yJpc3MiOiI0ZjUxY
0x00d0: 7a4d 344d 446b 7a5a 544d 305a 5459 3459 44mDlkM0ttY4ztlY
0x00e0: 5451 784e 5745 334e 4464 6d59 7a55 314e TQxNWE3NDdmYzU1N
0x00f0: 546b 784f 4349 7349 6d6c 6864 4349 364d TkxOCIsImlhdCI6M
0x0100: 5459 304d 7a59 304e 7a55 774e 5377 695a TY0MzY0NzUwNSwiZ
0x0110: 5868 7749 6a6f 784f 5455 354d 4441 334e XhwIjoxOTU5MDA3N
0x0120: 5441 3166 512e 4451 5354 6549 756f 4841 TA1fQ.DQSTeIuoHA
0x0130: 6547 2d4d 4763 4669 3945 3976 6a5f 3478 eG-MGcFi9E9vj_4x
0x0140: 4754 2d53 3946 6c44 4630 6a54 7754 3078 GT-S9FlDF0jTwT0x
0x0150: 450d 0a43 6f6e 7465 6e74 2d54 7970 653a E..Content-Type:
0x0160: 2061 7070 6c69 6361 7469 6f6e 2f6a 736f .application/jso
0x0170: 6e0d 0a0d 0a31 3730 3139 3738 3734 390d n....1701978749.
0x0180: 0a .
01:10:53.009501 IP docker-srv.local.8123 > 172.16.66.109.64018: Flags [.], ack 350, win 63891, length 0
0x0000: 4500 0028 30d4 4000 4006 36db c0a8 23fb E..(0.@.@.6...#.
0x0010: ac10 426d 1fbb fa12 3141 6086 0000 1b2f ..Bm....1A`..../
0x0020: 5010 f993 d33b 0000 P....;..
01:10:53.011628 IP docker-srv.local.8123 > 172.16.66.109.64018: Flags [P.], seq 1:186, ack 350, win 63891, length 185
0x0000: 4500 00e1 30d5 4000 4006 3621 c0a8 23fb E...0.@.@.6!..#.
0x0010: ac10 426d 1fbb fa12 3141 6086 0000 1b2f ..Bm....1A`..../
0x0020: 5018 f993 d3f4 0000 4854 5450 2f31 2e31 P.......HTTP/1.1
0x0030: 2034 3030 2042 6164 2052 6571 7565 7374 .400.Bad.Request
0x0040: 0d0a 436f 6e74 656e 742d 5479 7065 3a20 ..Content-Type:.
0x0050: 7465 7874 2f70 6c61 696e 3b20 6368 6172 text/plain;.char
0x0060: 7365 743d 7574 662d 380d 0a43 6f6e 7465 set=utf-8..Conte
0x0070: 6e74 2d4c 656e 6774 683a 2031 360d 0a44 nt-Length:.16..D
0x0080: 6174 653a 2057 6564 2c20 3032 2046 6562 ate:.Wed,.02.Feb
0x0090: 2032 3032 3220 3031 3a31 303a 3533 2047 .2022.01:10:53.G
0x00a0: 4d54 0d0a 5365 7276 6572 3a20 5079 7468 MT..Server:.Pyth
0x00b0: 6f6e 2f33 2e39 2061 696f 6874 7470 2f33 on/3.9.aiohttp/3
0x00c0: 2e37 2e34 2e70 6f73 7430 0d0a 436f 6e6e .7.4.post0..Conn
0x00d0: 6563 7469 6f6e 3a20 636c 6f73 650d 0a0d ection:.close...
0x00e0: 0a