SYNOPSIS

  use HTTP::Request::Common;
  $ua = LWP::UserAgent->new;
  $ua->request(GET 'http://www.sn.no/');
  $ua->request(POST 'http://somewhere/foo', [foo => bar, bar => foo]);

DESCRIPTION

This module provide functions that return newly created \*(C`HTTP::Request\*(C' objects. These functions are usually more convenient to use than the standard \*(C`HTTP::Request\*(C' constructor for the most common requests. The following functions are provided: The \s-1GET\s0() function returns an \*(C`HTTP::Request\*(C' object initialized with the \*(L"\s-1GET\s0\*(R" method and the specified \s-1URL\s0. It is roughly equivalent to the following call HTTP::Request->new( GET => $url, HTTP::Headers->new(Header => Value,...), ) but is less cluttered. What is different is that a header named \*(C`Content\*(C' will initialize the content part of the request instead of setting a header field. Note that \s-1GET\s0 requests should normally not have a content, so this hack makes more sense for the \s-1PUT\s0() and \s-1POST\s0() functions described below. The get(...) method of \*(C`LWP::UserAgent\*(C' exists as a shortcut for $ua->request(\s-1GET\s0 ...). Like \s-1GET\s0() but the method in the request is \*(L"\s-1HEAD\s0\*(R". The head(...) method of \*(L"LWP::UserAgent\*(R" exists as a shortcut for $ua->request(\s-1HEAD\s0 ...). Like \s-1GET\s0() but the method in the request is \*(L"\s-1PUT\s0\*(R". The content of the request can be specified using the \*(L"Content\*(R" pseudo-header. This steals a bit of the header field namespace as there is no way to directly specify a header that is actually called \*(L"Content\*(R". If you really need this you must update the request returned in a separate statement. Like \s-1GET\s0() but the method in the request is \*(L"\s-1DELETE\s0\*(R". This function is not exported by default. This works mostly like \s-1PUT\s0() with \*(L"\s-1POST\s0\*(R" as the method, but this function also takes a second optional array or hash reference parameter $form_ref. As for \s-1PUT\s0() the content can also be specified directly using the \*(L"Content\*(R" pseudo-header, and you may also provide the $form_ref this way. The $form_ref argument can be used to pass key/value pairs for the form content. By default we will initialize a request using the \*(C`application/x-www-form-urlencoded\*(C' content type. This means that you can emulate an \s-1HTML\s0 <form> POSTing like this: POST 'http://www.perl.org/survey.cgi', [ name => 'Gisle Aas', email => '[email protected]', gender => 'M', born => '1964', perc => '3%', ]; This will create an HTTP::Request object that looks like this: POST http://www.perl.org/survey.cgi Content-Length: 66 Content-Type: application/x-www-form-urlencoded

name=Gisle%20Aas&email=gisle%40aas.no&gender=M&born=1964&perc=3%25 Multivalued form fields can be specified by either repeating the field name or by passing the value as an array reference. The \s-1POST\s0 method also supports the \*(C`multipart/form-data\*(C' content used for Form-based File Upload as specified in \s-1RFC\s0 1867. You trigger this content format by specifying a content type of 'form-data' as one of the request headers. If one of the values in the $form_ref is an array reference, then it is treated as a file part specification with the following interpretation: [ $file, $filename, Header => Value... ] [ undef, $filename, Header => Value,..., Content => $content ] The first value in the array ($file) is the name of a file to open. This file will be read and its content placed in the request. The routine will croak if the file can't be opened. Use an \*(C`undef\*(C' as $file value if you want to specify the content directly with a \*(C`Content\*(C' header. The $filename is the filename to report in the request. If this value is undefined, then the basename of the $file will be used. You can specify an empty string as $filename if you want to suppress sending the filename when you provide a $file value. If a $file is provided by no \*(C`Content-Type\*(C' header, then \*(C`Content-Type\*(C' and \*(C`Content-Encoding\*(C' will be filled in automatically with the values returned by LWP::MediaTypes::guess_media_type() Sending my ~/.profile to the survey used as example above can be achieved by this: POST 'http://www.perl.org/survey.cgi', Content_Type => 'form-data', Content => [ name => 'Gisle Aas', email => '[email protected]', gender => 'M', born => '1964', init => ["$ENV{HOME}/.profile"], ] This will create an HTTP::Request object that almost looks this (the boundary and the content of your ~/.profile is likely to be different): POST http://www.perl.org/survey.cgi Content-Length: 388 Content-Type: multipart/form-data; boundary="6G+f"

--6G+f Content-Disposition: form-data; name="name"

Gisle Aas --6G+f Content-Disposition: form-data; name="email"

[email protected] --6G+f Content-Disposition: form-data; name="gender"

M --6G+f Content-Disposition: form-data; name="born"

1964 --6G+f Content-Disposition: form-data; name="init"; filename=".profile" Content-Type: text/plain

PATH=/local/perl/bin:$PATH export PATH

--6G+f-- If you set the $DYNAMIC_FILE_UPLOAD variable (exportable) to some \s-1TRUE\s0 value, then you get back a request object with a subroutine closure as the content attribute. This subroutine will read the content of any files on demand and return it in suitable chunks. This allow you to upload arbitrary big files without using lots of memory. You can even upload infinite files like /dev/audio if you wish; however, if the file is not a plain file, there will be no Content-Length header defined for the request. Not all servers (or server applications) like this. Also, if the file(s) change in size between the time the Content-Length is calculated and the time that the last chunk is delivered, the subroutine will \*(C`Croak\*(C'. The post(...) method of \*(L"LWP::UserAgent\*(R" exists as a shortcut for $ua->request(\s-1POST\s0 ...).

RELATED TO HTTP::Request::Common…

HTTP::Request, LWP::UserAgent

COPYRIGHT

Copyright 1997-2004, Gisle Aas

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.