Home assistant httpclient HELP!

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

Hi, and welcome to the community!
Here are my thoughts:

  • have you tried to do this with curl?
  • if so, what if you capture that?
  • and then, what if you compare that capture with what is coming out of the Particle device?

tip: I remember there is a right click-> follow tcp stream or similar in wireshark, so if you were to open your traces in wireshark and use that feature, you may find what is wrong with the http request.

Cheers,
Gustavo.

Thank you!

I have tried with curl and it works.Below is the wire shark capture:

The differences:

Broken (httpclient):
“P…/api/se” TOP
“.7.4.post0…Connection:.close…” BOTTOM

Working (curl):
“.T…POST./api/services/switch/turn_on.HTTP/1.1” TOP
“.Python/3.9.aiohttp/3.7.4.post” BOTTOM

    0x0000:  4500 01d0 9e59 4000 4006 d184 c0a8 23fe  E....Y@.@.....#.
    0x0010:  c0a8 23fb bda6 1fbb cefd 9d52 d77a 8dee  ..#........R.z..
    0x0020:  8018 01f6 cb0c 0000 0101 080a ca97 75c6  ..............u.
    0x0030:  ee54 bf8e 504f 5354 202f 6170 692f 7365  .T..POST./api/se
    0x0040:  7276 6963 6573 2f73 7769 7463 682f 7475  rvices/switch/tu
    0x0050:  726e 5f6f 6e20 4854 5450 2f31 2e31 0d0a  rn_on.HTTP/1.1..
    0x0060:  486f 7374 3a20 3139 322e 3136 382e 3335  Host:.192.168.35
    0x0070:  2e32 3531 3a38 3132 330d 0a55 7365 722d  .251:8123..User-
    0x0080:  4167 656e 743a 2063 7572 6c2f 372e 3831  Agent:.curl/7.81
    0x0090:  2e30 0d0a 4163 6365 7074 3a20 2a2f 2a0d  .0..Accept:.*/*.
    0x00a0:  0a41 7574 686f 7269 7a61 7469 6f6e 3a20  .Authorization:.
    0x00b0:  4265 6172 6572 2065 794a 3065 5841 694f  Bearer.eyJ0eXAiO
    0x00c0:  p 4b56 3151 694c 434a 6862 4763 694f  iJKV1QiLCJhbGciO
    0x00d0:  694a 4955 7a49 314e 694a 392e 6579 4a70  iJIUzI1NiJ9.eyJp
    0x00e0:  6333 4d69 4f69 4930 5a6a 5578 597a 4d34  c3MiOiI0ZjUxYzM4
    0x00f0:  4d44 6b7a 5a54 4d30 5a54 5934 5954 5178  MDkzZTM0ZTY4YTQx
    0x0100:  4e57 4533 4e44 646d 597a 5531 4e54 6b78  NWE3NDdmYzU1NTkx
    0x0110:  4f43 4973 496d 6c68 6443 4936 4d54 5930  OCIsImlhdCI6MTY0
    0x0120:  4d7a 5930 4e7a 5577 4e53 7769 5a58 6877  MzY0NzUwNSwiZXhw
    0x0130:  496a 6f78 4f54 5535 4d44 4133 4e54 4131  IjoxOTU5MDA3NTA1
    0x0140:  6651 2e44 5153 5465 4975 6f48 4165 472d  fQ.DQSTeIuoHAeG-
    0x0150:  4d47 6346 6939 4539 766a 5f34 7847 542d  MGcFi9E9vj_4xGT-
    0x0160:  5339 466c 4446 306a 5477 5430 7845 0d0a  S9FlDF0jTwT0xE..
    0x0170:  436f 6e74 656e 742d 5479 7065 3a20 6170  Content-Type:.ap
    0x0180:  706c 6963 6174 696f 6e2f 6a73 6f6e 0d0a  plication/json..
    0x0190:  436f 6e74 656e 742d 4c65 6e67 7468 3a20  Content-Length:.
    0x01a0:  3432 0d0a 0d0a 7b22 656e 7469 7479 5f69  42....{"entity_i
    0x01b0:  6422 3a20 2273 7769 7463 682e 6c69 7669  d":."switch.livi
    0x01c0:  6e67 5f72 6f6f 6d5f 6865 6174 6572 227d  ng_room_heater"}

21:50:25.313858 IP docker-srv.local.8123 > 192.168.35.254.48550: Flags [.], ack 413, win 506, options [nop,nop,TS val 3998531471 ecr 3398923718], length 0
0x0000: 4500 0034 b78a 4000 4006 b9ef c0a8 23fb E…4…@.@…#.
0x0010: c0a8 23fe 1fbb bda6 d77a 8dee cefd 9eee …#…z…
0x0020: 8010 01fa c970 0000 0101 080a ee54 bf8f …p…T…
0x0030: ca97 75c6 …u.
21:50:25.325031 IP docker-srv.local.8123 > 192.168.35.254.48550: Flags [P.], seq 1:148, ack 413, win 506, options [nop,nop,TS val 3998531482 ecr 3398923718], length 147
0x0000: 4500 00c7 b78b 4000 4006 b95b c0a8 23fb E…@.@…[…#.
0x0010: c0a8 23fe 1fbb bda6 d77a 8dee cefd 9eee …#…z…
0x0020: 8018 01fa ca03 0000 0101 080a ee54 bf9a …T…
0x0030: ca97 75c6 4854 5450 2f31 2e31 2032 3030 …u.HTTP/1.1.200
0x0040: 204f 4b0d 0a43 6f6e 7465 6e74 2d54 7970 .OK…Content-Typ
0x0050: 653a 2061 7070 6c69 6361 7469 6f6e 2f6a e:.application/j
0x0060: 736f 6e0d 0a43 6f6e 7465 6e74 2d4c 656e son…Content-Len
0x0070: 6774 683a 2032 0d0a 4461 7465 3a20 5475 gth:.2…Date:.Tu
0x0080: 652c 2030 3120 4665 6220 3230 3232 2032 e,.01.Feb.2022.2
0x0090: 313a 3530 3a32 3520 474d 540d 0a53 6572 1:50:25.GMT…Ser
0x00a0: 7665 723a 2050 7974 686f 6e2f 332e 3920 ver:.Python/3.9.
0x00b0: 6169 6f68 7474 702f 332e 372e 342e 706f aiohttp/3.7.4.po
0x00c0: 7374 300d 0a0d 0a st0…

Thoughts?

I would do this and compare in a text compare tool the outcome text:

right click-> follow tcp stream or similar in wireshark

that will give you a clearer view like this:

then you have better chances at finding the difference/problem.
Let me know

EDIT: how to