Shop OBEX P1 Docs P2 Docs Learn Events
How does a POST header work? — Parallax Forums

How does a POST header work?

RforbesRforbes Posts: 281
edited 2013-04-11 15:15 in Accessories
Hey all,

As usual, I've learned a lot from this forum and continue to try and learn new things.

I'm currently trying to work out an issue using POST with my spinneret. I'm not using Mike's HTTPServer for this, because it's an endeavor to learn, not to produce something valuable.

I've got a form made on a server...
<form action="connect.php" method="post">
  <div><label for="thename"> Name Value:
    <input type="text" name="thename" id="thename"></label>
  </div>
  <div><input type="submit" value="submit"></div>
</form>

And I'm attempting to connect to the server and send a POST header with the name/value pairs for the form. The idea is to automatically fill in the form with the spinneret.

My POST header is being created from my DAT section and looks like this:
  Post_Hdr              byte    "Post /test.php  HTTP/1.1 200 OK",13,10,            {           
}                               "Keep-Alive: 300",13,10,                                {           
}                               "Host: rcc.cfbtechnologies.com",13,10,                  {           
}                               "User-Agent: Wiz5100",13,10,                            {           
}                               "Cache-Control: public",13,10,                          {           
}                               "Connection: keep-alive",13,10,                         {           
}                               "Content-Type: application/x-www-form-urlencoded",13,10,{           
}                               "Content-length: ",0                                    {         
}

Once I put this into the txdata buffer, I then calculate the "Content-Length" of the name/value pairs and convert that value to a string. Then I put that into the txdata buffer.

Then, I put my name/value pairs into the buffer and xmit..

In a nut shell, my txdata buffer looks like the following:
Post /test.php  HTTP/1.1 200 OK

Keep-Alive: 300

Host: rcc.cfbtechnologies.com

User-Agent: Wiz5100

Cache-Control: public

Connection: keep-alive

Content-Type: application/x-www-form-urlencoded

Content-length: 25



thename=xxx&action=submit

When I look at the response, I get the following:
HTTP/1.1 200 OK

Date: Wed, 10 Apr 2013 23:06:00 GMT

Server: Apache

Vary: Accept-Encoding

Content-Length: 210

Keep-Alive: timeout=2, max=100

Connection: Keep-Alive

Content-Type: text/html



<form action="connect.php" method="post">
                                           <div><
label for="thename"> Name Value:
                                    <input type="
text" name="thename" id="thename"></label>
                                            </div
>
   <div><input type="submit" value="submit"></div
>
 </form>

I think my problem is how I'm forming my name/value pairs..... I have no clue how to force the "submit" button to "submit" and I'm not sure I've formed up the name/value pairs correctly.

Can someone show me what should be sent here???

Thanks much!
Robert

Comments

  • Mike GMike G Posts: 2,702
    edited 2013-04-10 18:53
    Your HTTP looks okay. You received a response so the server did not complain.

    If the response does not contain the name value pairs, I would suspect your PHP script is not doing what you expect. Post your PHP script.

    I'd get the PHP stuff woring with your browser before trying this with the Spinneret. That way you can remove some of the moving parts.
  • RforbesRforbes Posts: 281
    edited 2013-04-10 19:09
    Mike- right on... ok. I'll try to be clear about what I've done- please bare with me!! :)

    This is the file I'm trying to post to with the spinneret. test.php (I already posted it above, but here it is again.)
    <form action="connect.php" method="post">
      <div><label for="thename"> Name Value:
        <input type="text" name="thename" id="thename"></label>
      </div>
      <div><input type="submit" value="submit"></div>
    </form>
    

    Here is the connect.php that the file above posts to once the form is "submitted."
    <?php
    
    //This is called by test.php
    //This guy is just a basic connection script. It connects me to the database named robert as user robert.
    //Displays a pretty good error message if something goes wrong.
     
    try
    {
      $pdo = new PDO('mysql:host=mysql.xxx.abcyadda.com', 'user_name','awesomepassword');
      $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      $pdo->exec('SET NAMES "utf8"');
    }
    catch (PDOException $e)
    {
      $output = 'Woops! No connection to db server: --->   '.$e->getMessage();
      include 'output.html.php';
      exit();
    }
    try
    {
      $sql = 'INSERT INTO forbesdata.basic SET Name =:thename';
      $s = $pdo->prepare($sql);
      $s->bindValue(':thename',$_POST['thename']);
      $s->execute();
    }
    catch (PDOException $e)
    {
      $output = 'Error performing update:  '  . $e->getMessage();
      include 'output.html.php';
      exit();
    }
    $output = "Ok.";
    include 'output.html.php';
    ?>
    

    and the only other file involved is the output.html.php file which is:
    <!DOCTYPE html>
    <html lang="en">
      <Head>
        <meta charset="utf-8">
        <title>Script Output</title>
      </head>
      <body>
        <p>
          <?php echo $output; ?>
        </p>
      </body>
    </html> 
    

    When I go to test.php on my server, it renders as expected. I enter a value for thename and click the submit button, and it calls connect.php which then updates the database by inserting a new record into it's table. Works great, no issues at all.

    When I try to use the spinneret to post to the test.php file, I get what I showed above.

    If I try to just post to the connect.php file instead, I get an error telling me that "thename" cannot be null. So I'm thinking it's something to do with my POST header on the spinneret but I'm pretty much at a loss at this point.
  • Mike GMike G Posts: 2,702
    edited 2013-04-10 20:39
    You have to post to connect.php since connect.php contains the logic; $_POST.

    The header looks find but there could a problem I do not see. I would do something pretty simple. Create a new page called posttest.php. Then simply render form data back to the Spinneret; echo "data =: " . $_POST;.

    Consider downloading Fiddler. It's a great and free HTTP debugging tool which lets you see http headers. BTW, for quick header views I use Firefox and the HttpFox plugin. Take a look at the working code and use it as a template.
  • RforbesRforbes Posts: 281
    edited 2013-04-11 06:27
    Mike,

    Thanks for the pointers! I installed the HttpFox plugin and by looking at the headers I was able to modify the spinneret header appropriately. Seems that by adding ""Accept: text/html" to my header it's working fine now. I wish I could find a concise reference to all the junk that can be put into a header, with descriptions and definitions of what each piece does.
  • Mike GMike G Posts: 2,702
    edited 2013-04-11 09:28
    Well, ya have to start reading the RFCs. The RFCs are/were my primary references for understanding the standard HTTP protocol. Just try not to fall asleep.
    http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

    How browsers deal with the information is another discussion.
  • RforbesRforbes Posts: 281
    edited 2013-04-11 15:15
    Uhhh.... yep. The good ole RFC's. Not too terrible when one has nothing better to do, like stab oneself in the eye with a spork or play "count the clovers" in the yard or try to create diamonds by sticking coal in ones ear REALLY hard and tight and holding it there.

    C'mon man.... I sent "concise reference" not "mental torture device" ya know? ;)
Sign In or Register to comment.