Hello,
I am trying to use the HTTPClient on a ESP8266 to call some services in HA.
I have the curl command running ok, but my code in arduino is not working with HA. The authentication fails.
This is the curl command:
curl -X POST -H "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiIyYjU0NDA2MzYwMjY0ODM5OTk4ZGEzYTE2OGYxNDQxMCIsImlhdCI6MTU1NDA1NDA1OSwiZXhwIjoxODY5NDE0MDU5fQ.mIB3OVNLbZ0fZOyvpvvjXIJpLAJBybd7r-c497pAYjk" -H "Content-Type: application/json" -d '{"entity_id": "input_boolean.keep_ventilation_on"}' http://192.168.1.20:8123/api/services/input_boolean/toggle
And this is the relevant parts of the arduino code:
#include <ESP8266HTTPClient.h>
HTTPClient http;
http.begin("http://192.168.1.20:8123/api/services/input_boolean/toggle");
http.addHeader("Authorization:", "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiIyYjU0NDA2MzYwMjY0ODM5OTk4ZGEzYTE2OGYxNDQxMCIsImlhdCI6MTU1NDA1NDA1OSwiZXhwIjoxODY5NDE0MDU5fQ.mIB3OVNLbZ0fZOyvpvvjXIJpLAJBybd7r-c497pAYjk");
http.addHeader("Content-Type:", "application/json");
http.POST("{\"entity_id\": \"input_boolean.keep_ventilation_on\"}");
http.end();
The arduino code works if I add trusted_networks in configuration.yaml, but this is just avoiding the issue.
But this also shows that there might be a problem on how the Authorization header is being passed.
Does anyone see what I have done wrong?
Thanks
debsahu
(Debashish Sahu)
April 1, 2019, 5:04pm
2
Add -v
to curl to see what it is sending?
Here is the command and the output:
curl -X POST -H "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiIyYjU0NDA2MzYwMjY0ODM5OTk4ZGEzYTE2OGYxNDQxMCIsImlhdCI6MTU1NDA1NDA1OSwiZXhwIjoxODY5NDE0MDU5fQ.mIB3OVNLbZ0fZOyvpvvjXIJpLAJBybd7r-c497pAYjk" -H "Content-Type: application/json" -d '{"entity_id": "input_boolean.keep_ventilation_on"}' http://192.168.1.20:8123/api/services/input_boolean/toggle -v
Note: Unnecessary use of -X or --request, POST is already inferred.
* Trying 192.168.1.20...
* TCP_NODELAY set
* Connected to 192.168.1.20 (192.168.1.20) port 8123 (#0)
> POST /api/services/input_boolean/toggle HTTP/1.1
> Host: 192.168.1.20:8123
> User-Agent: curl/7.50.3
> Accept: */*
> Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiIyYjU0NDA2MzYwMjY0ODM5OTk4ZGEzYTE2OGYxNDQxMCIsImlhdCI6MTU1NDA1NDA1OSwiZXhwIjoxODY5NDE0MDU5fQ.mIB3OVNLbZ0fZOyvpvvjXIJpLAJBybd7r-c497pAYjk
> Content-Type: application/json
> Content-Length: 50
>
* upload completely sent off: 50 out of 50 bytes
< HTTP/1.1 200 OK
< Content-Type: application/json
< Content-Length: 371
< Date: Mon, 01 Apr 2019 17:07:00 GMT
< Server: Python/3.7 aiohttp/3.5.4
<
* Curl_http_done: called premature == 0
* Connection #0 to host 192.168.1.20 left intact
[{"attributes": {"friendly_name": "Ha p\u00e5 ventilasjon", "icon": "mdi:fan"}, "context": {"id": "ec38b296e60b4e4c953eb200b972ffb7", "parent_id": null, "user_id": "4911b011c78d4dcd99dd9786814cb1b6"}, "entity_id": "input_boolean.keep_ventilation_on", "last_changed": "2019-04-01T17:07:00.580597+00:00", "last_updated": "2019-04-01T17:07:00.580597+00:00", "state": "off"}]
debsahu
(Debashish Sahu)
April 1, 2019, 5:25pm
5
Add these lines:
#include <ESP8266HTTPClient.h>
HTTPClient http;
http.begin("http://192.168.1.20:8123/api/services/input_boolean/toggle");
http.addHeader("Authorization:", "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiIyYjU0NDA2MzYwMjY0ODM5OTk4ZGEzYTE2OGYxNDQxMCIsImlhdCI6MTU1NDA1NDA1OSwiZXhwIjoxODY5NDE0MDU5fQ.mIB3OVNLbZ0fZOyvpvvjXIJpLAJBybd7r-c497pAYjk");
http.addHeader("Content-Type:", "application/json");
http.addHeader("Accept: */*");
http.addHeader("Content-Length: 50");
http.POST("{\"entity_id\": \"input_boolean.keep_ventilation_on\"}");
String payload = http.getString();
http.end();
Cant see anything obvious.
gonzotek
(Gonzotek)
April 1, 2019, 5:43pm
7
I’m a little out of my league here, but reading the cpp source(Arduino/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp at master · esp8266/Arduino · GitHub ), I think you don’t need to include the colon in the addheader functions:
http.addHeader(“Content-Type”, “application/json”);
There’s also a setAuthorization functions you might need to investigate. I haven’t worked with ESP8266HTTPClient before, so this is just a wild shot in the dark
I must say you are a very good shooter!
I read that line but my brain did not comprehend it.
Removed the colon as you said and now it works!
Thanks alot, both of you!
1 Like
debsahu
(Debashish Sahu)
April 1, 2019, 5:50pm
9
So this is the solution?
#include <ESP8266HTTPClient.h>
HTTPClient http;
http.begin("http://192.168.1.20:8123/api/services/input_boolean/toggle");
http.addHeader("Authorization", "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiIyYjU0NDA2MzYwMjY0ODM5OTk4ZGEzYTE2OGYxNDQxMCIsImlhdCI6MTU1NDA1NDA1OSwiZXhwIjoxODY5NDE0MDU5fQ.mIB3OVNLbZ0fZOyvpvvjXIJpLAJBybd7r-c497pAYjk");
http.addHeader("Content-Type", "application/json");
http.addHeader("Accept","*/*");
http.addHeader("Content-Length","50");
http.POST("{\"entity_id\": \"input_boolean.keep_ventilation_on\"}");
String payload = http.getString();
http.end();
Almost, I did not need the Accept and Content-Length headers.
So this is the code that works:
#include <ESP8266HTTPClient.h>
HTTPClient http;
http.begin("http://192.168.1.20:8123/api/services/input_boolean/toggle");
http.addHeader("Authorization", "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiIyYjU0NDA2MzYwMjY0ODM5OTk4ZGEzYTE2OGYxNDQxMCIsImlhdCI6MTU1NDA1NDA1OSwiZXhwIjoxODY5NDE0MDU5fQ.mIB3OVNLbZ0fZOyvpvvjXIJpLAJBybd7r-c497pAYjk");
http.addHeader("Content-Type", "application/json");
http.POST("{\"entity_id\": \"input_boolean.keep_ventilation_on\"}");
http.end();