SendToHTTP (GET and POST) with Headers

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
iz8mbw
Normal user
Posts: 35
Joined: 14 Apr 2023, 08:43
Location: Napoli, Italy

SendToHTTP (GET and POST) with Headers

#1 Post by iz8mbw » 05 Sep 2023, 10:19

Hello.
I'm doing some experience with "SendToHTTP ", for me it's very useful.
I need to send some HTTP GET and HTTP POST with Headers, is it possible?

Here some "curl" examples that I would like to "convert" for ESPEasy:

GET:
curl -X GET -H "Authorization: Bearer VERY_LOG_TOKEN" -H "Content-Type: application/json" http://192.168.1.25:8123/api/states/sen ... r_humidity

POST:
curl -X POST -H "Authorization: Bearer VERY_LOG_TOKEN" -H "Content-Type: application/json" -d '{"state": "off"}' http://192.168.1.25:8123/api/states/switch.tapo1


Many thanks!

User avatar
Ath
Normal user
Posts: 3522
Joined: 10 Jun 2018, 12:06
Location: NL

Re: SendToHTTP (GET and POST) with Headers

#2 Post by Ath » 05 Sep 2023, 10:23

Check out the PostToHTTP and PutToHTTP (and SendToHTTP) commands in the docs :)
/Ton (PayPal.me)

iz8mbw
Normal user
Posts: 35
Joined: 14 Apr 2023, 08:43
Location: Napoli, Italy

Re: SendToHTTP (GET and POST) with Headers

#3 Post by iz8mbw » 05 Sep 2023, 10:46

Thanks, trying to "convert" the curl format with the ESPEasy format but it's hard...
Why don't use the same "curl" syntax like: -H "header 1 value" -H "header 2 value" so simply separate Headers by space.

In ESPEasy the

Code: Select all

%LF%
is the Header separator like the space for curl?

User avatar
Ath
Normal user
Posts: 3522
Joined: 10 Jun 2018, 12:06
Location: NL

Re: SendToHTTP (GET and POST) with Headers

#4 Post by Ath » 05 Sep 2023, 11:00

Headers have to be on a separate line, AFAICS, so there has to be some separator to be able to split multiple value pairs, and we chose %LF% to do that, as most other characters can be part of the header value. %LF% is the equivalent of '\n' in C++ ;)
/Ton (PayPal.me)

iz8mbw
Normal user
Posts: 35
Joined: 14 Apr 2023, 08:43
Location: Napoli, Italy

Re: SendToHTTP (GET and POST) with Headers

#5 Post by iz8mbw » 08 Sep 2023, 15:12

Hello.
May be can be of interest for other users. I post the original CURL command and the equivalent command (PostToHTTP) for ESPEasy rule:

HTTP POST with curl:

Code: Select all

curl -X POST -H "Authorization: Bearer VERY_LONG_HOME_ASSISTANT_TOKEN_TO_VALORIZE" -H "Content-Type: application/json" -d '{"state": "off"}' http://192.168.1.25:8123/api/states/light.shellyplus1pm_123456abc123_switch_0
HTTP POST with ESPEasy:

Code: Select all

PostToHTTP,192.168.1.25,8123,/api/states/light.shellyplus1pm_123456abc123_switch_0,'Authorization: Bearer VERY_LONG_HOME_ASSISTANT_TOKEN_TO_VALORIZE%LF%Content-Type: application/json',`{"state": "off"}`

User avatar
Ath
Normal user
Posts: 3522
Joined: 10 Jun 2018, 12:06
Location: NL

Re: SendToHTTP (GET and POST) with Headers

#6 Post by Ath » 09 Sep 2023, 10:13

To make the conversion even easier, you can use the documented 'format 2' use of PostToHTTP, where a regular http:// url can be used.

Alternative HTTP POST with ESPEasy:

Code: Select all

PostToHTTP,http://192.168.1.25:8123/api/states/light.shellyplus1pm_123456abc123_switch_0,'Authorization: Bearer VERY_LONG_HOME_ASSISTANT_TOKEN_TO_VALORIZE%LF%Content-Type: application/json',`{"state": "off"}`
/Ton (PayPal.me)

User avatar
Ath
Normal user
Posts: 3522
Joined: 10 Jun 2018, 12:06
Location: NL

Re: SendToHTTP (GET and POST) with Headers

#7 Post by Ath » 09 Sep 2023, 22:23

NB: I've added this as an example to the Rules documentation via PR #4788
/Ton (PayPal.me)

iz8mbw
Normal user
Posts: 35
Joined: 14 Apr 2023, 08:43
Location: Napoli, Italy

Re: SendToHTTP (GET and POST) with Headers

#8 Post by iz8mbw » 11 Sep 2023, 14:02

Great, thanks!

Post Reply

Who is online

Users browsing this forum: No registered users and 31 guests