Home > Articles > Web Development > Perl

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Reading POSTed Data Manually

You need to read data sent by the POST or PUT method that is not submitted in application/x-www-form-urlencoded or multipart/form-data format.

Technique

Use the read() method from the Apache class to read the submitted data.

sub handler {

 my $r = shift;

 my $content;

 $r->read($content, $r->header_in('Content-length'));

 $r->send_http_header('text/html');

 $r->print("<html><body>\n");
 $r->print("<h1>Reading data</h1>\n");

 my (@pairs) = split(/[&;]/, $content);

 foreach my $pair (@pairs) {
  my ($parameter, $value) = split('=', $pair, 2);
  $r->print("$parameter has value $value<br>\n");
 }

 $r->print("</body></html>\n");

 return OK;
}

Comments

As discussed in Recipe 3.5, data that has been submitted via POST can be read either with the args() method, the content() method, or for more flexibility, the param() method from the Apache::Request class. However, this works only if the request MIME type is application/x-www-form-urlencoded or multipart/form-data. For other MIME types you need to use Apache's read() method to access the submitted data. You might use this, for example, to read the data submitted by a PUT request.

To get the incoming message body data into a variable, pass a scalar and length to the read() method. If you want to read the entire submission, set the length to the value of the Content-Length header, if it exists, as done in the sample code.

Note that Apache, through the TimeOut directive, sets a value for a timeout that will abort processing if the client no longer responds. If you find yourself consistently getting timeout errors when reading in large files, you can set the TimeOut directive to a higher value, or modify the value directly through Apache::Server's timeout() method, as shown in Recipe 4.1.

  • + Share This
  • 🔖 Save To Your Account