Help with RESTful Command convert from python

From - RESTful Command - Home Assistant

I am trying to reproduce this python requests statement

requests.post('http://juke.local/zone_toggle.php', data={'index':1, 'id':'None', 'power':'off', 'source':3}) 

this is not working

 juke_s4_o1:
   url: http://10.10.10.13/zone_toggle.php
   method: post
   content_type: "application/x-www-form-urlencoded"
   payload: "{'index':0, 'id':'None', 'power':'off', 'source':4}"

been doing wireshark traces, postman, and others to learn what I do not know. My guess is that this is not a true RESTful API but instead I am filling in a form??

This is the Wireshark from a python script that works

Frame 5: 104 bytes on wire (832 bits), 104 bytes captured (832 bits)
Ethernet II, Src: Apple_89:78:bc (9c:76:0e:89:78:bc), Dst: Raspberr_e5:ec:f4 (b8:27:eb:e5:ec:f4)
802.1Q Virtual LAN, PRI: 0, DEI: 0, ID: 40
Internet Protocol Version 4, Src: 10.10.10.99, Dst: 10.10.10.13
Transmission Control Protocol, Src Port: 60956, Dst Port: 80, Seq: 227, Ack: 1, Len: 34
[2 Reassembled TCP Segments (260 bytes): #4(226), #5(34)]
Hypertext Transfer Protocol
    POST /zone_toggle.php HTTP/1.1\r\n
    Host: juke.local\r\n
    User-Agent: python-requests/2.27.1\r\n
    Accept-Encoding: gzip, deflate\r\n
    Accept: */*\r\n
    Connection: keep-alive\r\n
    Content-Length: 34\r\n
    Content-Type: application/x-www-form-urlencoded\r\n
    \r\n
    [Full request URI: http://juke.local/zone_toggle.php]
    [HTTP request 1/1]
    [Response in frame: 8]
    File Data: 34 bytes
HTML Form URL Encoded: application/x-www-form-urlencoded
    Form item: "index" = "0"
    Form item: "id" = "None"
    Form item: "power" = "off"
    Form item: "source" = "4"

My HA config yealds

Frame 6: 121 bytes on wire (968 bits), 121 bytes captured (968 bits)
Ethernet II, Src: Raspberr_8d:ae:be (dc:a6:32:8d:ae:be), Dst: Raspberr_e5:ec:f4 (b8:27:eb:e5:ec:f4)
802.1Q Virtual LAN, PRI: 0, DEI: 0, ID: 40
Internet Protocol Version 4, Src: 10.10.0.20, Dst: 10.10.10.13
Transmission Control Protocol, Src Port: 57524, Dst Port: 80, Seq: 229, Ack: 1, Len: 51
[2 Reassembled TCP Segments (279 bytes): #4(228), #6(51)]
Hypertext Transfer Protocol
    POST /zone_toggle.php HTTP/1.1\r\n
    Host: 10.10.10.13\r\n
    User-Agent: HomeAssistant/2022.4.7 aiohttp/3.8.1 Python/3.9\r\n
    Content-Type: application/x-www-form-urlencoded\r\n
    Accept: */*\r\n
    Accept-Encoding: gzip, deflate\r\n
    Content-Length: 51\r\n
    \r\n
    [Full request URI: http://10.10.10.13/zone_toggle.php]
    [HTTP request 1/1]
    [Response in frame: 8]
    File Data: 51 bytes
HTML Form URL Encoded: application/x-www-form-urlencoded
    Form item: "{'index':0, 'id':'None', 'power':'off', 'source':4}" = ""

Thank You in advance.

You’re asking for it.

If you want “plain” REST-JSON, use application/json

1 Like

@koying Thank You for the suggestion, but it did not work.

The PostMan and Python all show

HTML Form URL Encoded: application/x-www-form-urlencoded

in the request. I tried the

application/json

Did not work and the trace shows a different formated Post

Frame 5: 117 bytes on wire (936 bits), 117 bytes captured (936 bits)
Ethernet II, Src: Raspberr_8d:ae:be (dc:a6:32:8d:ae:be), Dst: Raspberr_e5:ec:f4 (b8:27:eb:e5:ec:f4)
802.1Q Virtual LAN, PRI: 0, DEI: 0, ID: 40
Internet Protocol Version 4, Src: 10.10.0.20, Dst: 10.10.10.13
Transmission Control Protocol, Src Port: 57866, Dst Port: 80, Seq: 224, Ack: 1, Len: 47
    Source Port: 57866
    Destination Port: 80
    [Stream index: 0]
    [Conversation completeness: Complete, WITH_DATA (31)]
    [TCP Segment Len: 47]
    Sequence Number: 224    (relative sequence number)
    Sequence Number (raw): 3640595095
    [Next Sequence Number: 271    (relative sequence number)]
    Acknowledgment Number: 1    (relative ack number)
    Acknowledgment number (raw): 2464486514
    1000 .... = Header Length: 32 bytes (8)
    Flags: 0x018 (PSH, ACK)
    Window: 502
    [Calculated window size: 64256]
    [Window size scaling factor: 128]
    Checksum: 0x49e5 [unverified]
    [Checksum Status: Unverified]
    Urgent Pointer: 0
    Options: (12 bytes), No-Operation (NOP), No-Operation (NOP), Timestamps
    [Timestamps]
    [SEQ/ACK analysis]
    TCP payload (47 bytes)
    TCP segment data (47 bytes)
[2 Reassembled TCP Segments (270 bytes): #4(223), #5(47)]
Hypertext Transfer Protocol
    POST /zone_toggle.php HTTP/1.1\r\n
    Host: 10.10.10.13\r\n
    User-Agent: HomeAssistant/2022.4.7 aiohttp/3.8.1 Python/3.9\r\n
    Content-Type: application/application/json\r\n
    Accept: */*\r\n
    Accept-Encoding: gzip, deflate\r\n
    Content-Length: 47\r\n
    \r\n
    [Full request URI: http://10.10.10.13/zone_toggle.php]
    [HTTP request 1/1]
    [Response in frame: 8]
    File Data: 47 bytes
Media Type
    Media type: application/application/json (47 bytes)

The data is in the packet, not on the decode.

I whish I knew {} verse verse () better, as it seems like it is a set issue. I need each data pair to be their own.

HTML Form URL Encoded: application/x-www-form-urlencoded
    Form item: "index" = "0"
    Form item: "id" = "None"
    Form item: "power" = "off"
    Form item: "source" = "4"

verse the send that HASS is sending

HTML Form URL Encoded: application/x-www-form-urlencoded
    Form item: "{'index':0, 'id':'None', 'power':'off', 'source':4}" = ""

Ah, misread that it must be x-www-form-urlencoded.

Then keep the content-type and format the payload as

index=0&id=None&power=off&source=4

Not sure where you got that you needed json, here

2 Likes

That doesn’t look right. Copy&paste issue maybe? application/json is the correct content type if your payload is formatted as JSON.

If you really want to use application/x-www-form-urlencoded then your payload must be formatted as key-value-pairs, such as index=0&id=None&power=off&source=4, but be aware that non-alphanumeric characters require special formatting.

1 Like

[[Edit]]
@exxamalte and @koying Thank You

   juke_s4_o2:
   url: http://10.10.10.13/zone_toggle.php
   method: post
   content_type: "application/x-www-form-urlencoded"
   payload: 'index=1&id=None&power=off&source=4'

is working.

formating the

“application/x-www-form-urlencoded”
with the special characters worked

Frame 5: 104 bytes on wire (832 bits), 104 bytes captured (832 bits)
Ethernet II, Src: Raspberr_8d:ae:be (dc:a6:32:8d:ae:be), Dst: Raspberr_e5:ec:f4 (b8:27:eb:e5:ec:f4)
802.1Q Virtual LAN, PRI: 0, DEI: 0, ID: 40
Internet Protocol Version 4, Src: 10.10.0.20, Dst: 10.10.10.13
Transmission Control Protocol, Src Port: 40936, Dst Port: 80, Seq: 229, Ack: 1, Len: 34
    Source Port: 40936
    Destination Port: 80
    [Stream index: 0]
    [Conversation completeness: Incomplete, DATA (15)]
    [TCP Segment Len: 34]
    Sequence Number: 229    (relative sequence number)
    Sequence Number (raw): 1439104942
    [Next Sequence Number: 263    (relative sequence number)]
    Acknowledgment Number: 1    (relative ack number)
    Acknowledgment number (raw): 526325523
    1000 .... = Header Length: 32 bytes (8)
    Flags: 0x018 (PSH, ACK)
    Window: 502
    [Calculated window size: 64256]
    [Window size scaling factor: 128]
    Checksum: 0xea89 [unverified]
    [Checksum Status: Unverified]
    Urgent Pointer: 0
    Options: (12 bytes), No-Operation (NOP), No-Operation (NOP), Timestamps
    [Timestamps]
    [SEQ/ACK analysis]
    TCP payload (34 bytes)
    TCP segment data (34 bytes)
[2 Reassembled TCP Segments (262 bytes): #4(228), #5(34)]
Hypertext Transfer Protocol
    POST /zone_toggle.php HTTP/1.1\r\n
    Host: 10.10.10.13\r\n
    User-Agent: HomeAssistant/2022.4.7 aiohttp/3.8.1 Python/3.9\r\n
    Content-Type: application/x-www-form-urlencoded\r\n
    Accept: */*\r\n
    Accept-Encoding: gzip, deflate\r\n
    Content-Length: 34\r\n
    \r\n
    [Full request URI: http://10.10.10.13/zone_toggle.php]
    [HTTP request 1/1]
    [Response in frame: 8]
    File Data: 34 bytes
HTML Form URL Encoded: application/x-www-form-urlencoded
    Form item: "index" = "2"
        Key: index
        Value: 2
    Form item: "id" = "None"
        Key: id
        Value: None
    Form item: "power" = "off"
        Key: power
        Value: off
    Form item: "source" = "4"
        Key: source
        Value: 4
1 Like