Home AMX User Forum NetLinx Studio

POST Command

Dear All,

what are the minimum requirements for a "POST" HTTP1.1 command ?

According to the P2 AMX Programming course, the corresponding "GET" one is like the below :

DEFINE_CONSTANT

char DUNE_COMMANDS[ ][8] =
{
(1) 'BC43BF00', //POWER
(2) 'EF10BF00', //EJECT
(3) 'BA45BF00', //MODE
(4) 'BF40BF00', //A
(5) 'E01FBF00', //B
(6) 'FF00BF00', //C
(7) 'BE41BF00', //D
(8) 'FA05BF00', //CLEAR
(9) 'BD42BF00' //SELECT
}

define_function sendDuneHttp(char iMode[20], char iCmd[50])
{
stack_var char iPacket[500];

// Minimum HTTP 1.1 Requirement
iPacket = "iMode,' ',iCmd,' HTTP/1.1',$0D,$0A,
      'Host: ',uDuneIpConnection.address,':',itoa(uDuneIpConnection.port),$0D,$0A,
      $0D,$0A";// HTTP terminates with the empty line

send_string dvDune,iPacket;
}

button_event[TPs_Dune, 0]
{
push: sendDuneHttp('GET', "'/cgi-bin/do?cmd=ir_code&ir_code=',DUNE_COMMANDS[button.input.channel]");
}

George

Comments

  • viningvining Posts: 4,368

    “ Description

    Quick java tutorial on Difference between GET and POST method in HTTP. Both GET and POST method is used to transfer data from client to server in HTTP protocol but Main difference between POST and GET method is that GET carries request parameter appended in URL string while POST carries request parameter in message body which makes it more secure way of transferring data from client to server in http protocol.“

    So according to his the command parameters aren’t in the URL string but the body of the message, unless you have an api you’ll need to get some wireshark intercepts to see how it should be formatted.

  • At minimum change the "GET" to "POST".
    All the Roku cursor controls from the video are POST

    button_event[dvTP_1_ROKU, 0]
    {
        push:
        {
        to[button.input];
        switch(button.input.channel)
        {
            case BTN_MENU_FUNC:
            {
            sendRokuHttp('POST', '/keypress/Home');
            }
            case BTN_MENU_UP:
            {
            sendRokuHttp('POST', '/keypress/Up');
            }
            case BTN_MENU_DN:
            {
            sendRokuHttp('POST', '/keypress/Down');
            }
            case BTN_MENU_LT:
            {
            sendRokuHttp('POST', '/keypress/Left');
            }
            case BTN_MENU_RT:
            {
            sendRokuHttp('POST', '/keypress/Right');
            }
            case BTN_MENU_SELECT:
            {
            sendRokuHttp('POST', '/keypress/Select');
            }
        }
        }
    }
    
  • Since it is not possible for the file to be uploaded here ( !!!! ), below you can see a part of the device commands (HOME BUTTON)...

    POST /cgi-bin/toServerValue.cgi HTTP/1.1\x0D\x0AContent-Length: 14\x0D\x0AContent-Type: text/plain; charset=ISO-8859-1\x0D\x0AHost: your-ip-address:3388\x0D\x0AConnection: Keep-Alive\x0D\x0A\x0D\x0A{"remote":"o"}

    In this case, it is not just a change from "GET" to "POST".

    Nevertheless, I'm sending the command like my "GET" example but following the above POST format and my device gets offline immediately after any command. The function is not always executed...

  • viningvining Posts: 4,368

    Make sure you put in the masters IP address where your-ip-address is and your content length is the length of the body string {“remote”:”o”} . 14 works for this command but you have to count the length of each command cuz that tells the server how much to graba nd parse after the header.

    Show the way your formatted the above message in AMX cuz that won’t fly as is

  • "your-ip-address" should be the device IP one...

    DEFINE_DEVICE

    dvTP = 10001:1:0;
    dvWD = 0:4:0;

    DEFINE_CONSTANT

    char WD_COMMANDS[ ][20] =
    {
    (1) '{"remote":"w"}', //power
    (2) '{"remote":","}', //audio
    (3) '{"remote":"M"}', //mute
    (4) '{"remote":"G"}', //option
    (5) '{"remote":"E"}', //search
    (6) '{"remote":"s"}', //setup
    (7) '{"remote":"\\"}', //subtitle
    (8) '{"remote":"X"}', //eject
    (9) '{"remote":"x"}', //A-green
    (10) '{"remote":"y"}', //B-red
    (11) '{"remote":"z"}', //C-yellow
    (12) '{"remote":"A"}', //D-blue
    (13) '{"remote":"o"}', //home
    (14) '{"remote":"T"}', //back
    (15) '{"remote":"D"}', //next page
    (16) '{"remote":"U"}', //previous page
    (17) '{"remote":"n"}', //ok/enter
    (18) '{"remote":"d"}', //cursor down
    (19) '{"remote":"I"}', //cursor left
    (20) '{"remote":"r"}', //cursor right
    (21) '{"remote":"u"}' //cursor up
    }

    DEFINE_TYPE

    structure _IpConnection
    {
    char address[255];
    integer port;
    char mode; // TCP or UDP
    char isConnected;
    }

    DEFINE_VARIABLE

    volatile _IpConnection uWDIpConnection;
    volatile char sWDIpQueue[1000];

    define_function sendWDHttp ( char iMode[20], char iCmd[20] )
    {
    local_var char iPacket[500];

    // Minimum HTTP 1.1 Requirement
    iPacket = "iMode,' /cgi-bin/toServerValue.cgi HTTP/1.1',$0D,$0A,
            'Content-Length: ',itoa(length_string(iCmd)),$0D,$0A,
            'Content-Type: text/plain; charset=ISO-8859-1',$0D,$0A,
            'Host: ',uWDIpConnection.address,':',itoa(uWDIpConnection.port),$0D,$0A,
            'Connection: Keep-Alive',$0D,$0A,
            $0D,$0A,
            iCmd";
    

    if(uWDIpConnection.isConnected == TRUE)
    {
    send_string dvWD,iPacket;
    }
    else
    {
    sWDIpQueue = "sWDIpQueue,iPacket";
    ip_client_open(dvWD.PORT, uWDIpConnection.address, uWDIpConnection.port, uWDIpConnection.mode);
    } }
    }

    DEFINE_EVENT

    button_event[dvTP, 0]
    {
    push: sendWDHttp('POST', WD_COMMANDS[button.input.channel]);
    }

    THE REPLY while trying to send the "RIGHT CURSOR" command:

    Line 11 2020-07-31 (17:00:17):: WD:string: HTTP/1.1 200 OK$0D$0A
    Line 12 2020-07-31 (17:00:17):: WD:string: Date: Fri, 31 Jul 2020 14:00:44 GMT$0D$0A
    Line 13 2020-07-31 (17:00:17):: WD:string: Server: Apache$0D$0A
    Line 14 2020-07-31 (17:00:17):: WD:string: Debug: haha$0D$0A
    Line 15 2020-07-31 (17:00:17):: WD:string: X-Orion-Version: 1.0$0D$0A
    Line 16 2020-07-31 (17:00:17):: WD:string: Content-Length: 17$0D$0A
    Line 17 2020-07-31 (17:00:17):: WD:string: Keep-Alive: timeout=5, max=100$0D$0A
    Line 18 2020-07-31 (17:00:17):: WD:string: Connection: Keep-Alive$0D$0A
    Line 19 2020-07-31 (17:00:17):: WD:string: Content-Type: text/html;charset=iso-8859-1;debug:[POST /cgi-bin/]$0D$0A
    Line 20 2020-07-31 (17:00:17):: WD:string: $0D$0A
    Line 21 2020-07-31 (17:00:17):: WD:string: { "success": 0 }$0A
    Line 22 2020-07-31 (17:00:17):: WD:string: <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">$0A
    Line 23 2020-07-31 (17:00:17):: WD:string: $0A
    Line 24 2020-07-31 (17:00:17):: WD:string: 400 Bad Request$0A
    Line 25 2020-07-31 (17:00:17):: WD:string: $0A
    Line 26 2020-07-31 (17:00:17):: WD:string:

    Bad Request

    $0A
    Line 27 2020-07-31 (17:00:17):: WD:string:

    Your browser sent a request that this server could not understand.
    $0A Line 28 2020-07-31 (17:00:17):: WD:string:

    $0A
    Line 29 2020-07-31 (17:00:17):: WD:string: $0A
    Line 30 2020-07-31 (17:00:17):: CIpEvent::OffLine 0:4:1

  • Line 16 2020-07-31 (17:00:17):: WD:string: Content-Length: 17$0D$0A

    Didn't follow this thread, but is length 17 correct for that command? Why are there (number) brackets in the WD_COMMANDS array? are they part of the actual command?

  • viningvining Posts: 4,368

    with stuff like this I usually create a local var in the function big enough for the entire string and send_string 0, var so I can see exactly what’s being sent, make sure every thing is correct, nothing getting cut off, check content length is what it should be, ip address is what it should be, etc. You can then also drop that var in debug to see what it is too if it’s not over 200 character there abouts. Then later once working make it a stackvar.

Sign In or Register to comment.