Click to See Complete Forum and Search --> : PERL support for POST and GET


bogocles
10-19-2007, 03:47 PM
I've been toying around with a test script that prints environment variables and provides two forms, each with either a post or get method. I've noticed that when I supply the same text in both the POST and the GET form, the GET form seems to take precedence as the associated environment variables get GET properties only. Most notably:

REQUEST_METHOD = GET
... and CONTENT_LENGTH isn't set.

The only way to get REQUEST_METHOD = POST is to submit a method="post" form without any GET information in the url.

This seems kinda like a drawback. I'm trying to port over a php application that I've developed but it sends information via POST and GET, which doesn't appear to work in PERL. It looks like you get either or.

Is this right? If it is, are there any work-arounds? Porting is headache enough without have to make major structural changes to the code :(

The test script I'm using is a slightly modified version of one from /www.gossland.com/. Here's the code if anyone wants to mess around with it:

#!/usr/bin/perl -w

use CGI::Carp qw ( warningsToBrowser fatalsToBrowser );

print "Content-type: text/html\n\n";

print qq{<html><head></head><body>};

#print every key in the environment

foreach $key ( sort ( keys %ENV ) ) {

print $key, ' = ', $ENV { $key }, "<br />\n";

}

#print a couple of simple forms: a POST form and a GET form

print qq {

<form method="POST" ><input type="submit" value="Post Request"><input name="postfield"></form>

};

print qq {

<form method="GET" ><input type="submit" value="Get Request "><input name="getfield"></form>

};

print qq {

</body></html>

};

bogocles
10-19-2007, 07:56 PM
RESOLVED

Figured out a way around this finally:

GET and POST requests can be passed along at the same time. Once a GET is sent (in the URL), perl puts the query into:

$ENV{ 'QUERY_STRING' }

ALSO, if the action of a POST method form includes GET information, once the request processes after the submit button is pressed, the following is set:

$ENV { 'REQUEST_METHOD' } = POST
$ENV { 'CONTENT_LENGTH' } = <length of information passed>

From there hashes can be made from $ENV{ 'QUERY_STRING' } (for GETs) and a read statement like:

read ( STDIN, $post_hash, $ENV{ 'CONTENT_LENGTH' } );

... for POSTs.

Happily, both sets of information can exist at the same time :)

I can post code for this if anyone wants it. Not sure if this topic really interests anyone else but me, but the answer to my question was pretty much nowhere to be found. Spent the better part of a day coming up with this.

- PERL newb

bluestartech
10-22-2007, 07:08 AM
reading form vars is quite easy with the CGI module for perl.

<input name="username">

--

#!/usr/bin/perl

use CGI qw(param);

$username = param('username');

---
As you can see the code is v concise and simplifies reading get and post vars since you can use the one fuction to get data no matter which method was used to submit form

Diarmuid Ryan
Blue Star Web Design, Tipperary, Ireland (http://www.bluestar.ie)