HTTP::Request::Common
NAME
VERSION
SYNOPSIS
DESCRIPTION
SEE ALSO
AUTHOR
COPYRIGHT AND LICENSE
NAME
HTTP::Request::Common − Construct common HTTP::Request objects
VERSION
version 6.26
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]);
$ua−>request(PATCH ‘http://somewhere/foo’, [foo => bar, bar => foo]);
$ua−>request(PUT ‘http://somewhere/foo’, [foo => bar, bar => foo]);
$ua−>request(OPTIONS ‘http://somewhere/foo’, [foo => bar, bar => foo]);
DESCRIPTION
This module provides functions that return newly created “HTTP::Request” objects. These functions are usually more convenient to use than the standard “HTTP::Request” constructor for the most common requests.
Note that LWP::UserAgent has several convenience methods, including “get”, “head”, “delete”, “post” and “put”.
The following functions are provided:
GET $url
GET $url, Header => Value,…
The “GET” function returns an HTTP::Request object initialized with the ” GET” method and the specified URL. 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 “Content” will initialize the content part of the request instead of setting a header field. Note that GET requests should normally not have a content, so this hack makes more sense for the “PUT”, “PATCH”
and “POST” functions described below.
The “get(…)” method of LWP::UserAgent exists as a shortcut for “$ua−>request(GET …)”.
HEAD $url
HEAD $url, Header => Value,…
Like GET () but the method in the request is ” HEAD”.
The “head(…)” method of LWP::UserAgent exists as a shortcut for “$ua−>request(HEAD …)”.
DELETE $url
DELETE $url, Header => Value,…
Like “GET” but the method in the request is “DELETE”. This function is not exported by default.
PATCH $url
PATCH $url, Header => Value,…
PATCH $url, $form_ref, Header => Value,…
PATCH $url, Header => Value,…, Content => $form_ref
PATCH $url, Header => Value,…, Content => $content
The same as “POST” below, but the method in the request is “PATCH”.
PUT $url
PUT $url, Header => Value,…
PUT $url, $form_ref, Header => Value,…
PUT $url, Header => Value,…, Content => $form_ref
PUT $url, Header => Value,…, Content => $content
The same as “POST” below, but the method in the request is “PUT”
OPTIONS $url
OPTIONS $url, Header => Value,…
OPTIONS $url, $form_ref, Header => Value,…
OPTIONS $url, Header => Value,…, Content => $form_ref
OPTIONS $url, Header => Value,…, Content => $content
The same as “POST” below, but the method in the request is “OPTIONS”
POST $url
POST $url, Header => Value,…
POST $url, $form_ref, Header => Value,…
POST $url, Header => Value,…, Content => $form_ref
POST $url, Header => Value,…, Content => $content
“POST”, “PATCH” and “PUT” all work with the same parameters.
%data = ( title => ‘something’, body => something else’ );
$ua = LWP::UserAgent−>new();
$request = HTTP::Request::Common::POST( $url, [ %data ] );
$response = $ua−>request($request);
They take a second optional array or hash reference parameter $form_ref. The content can also be specified directly using the “Content” pseudo-header, and you may also provide the $form_ref this way.
The “Content” pseudo-header steals a bit of the header field namespace as there is no way to directly specify a header that is actually called “Content”. If you really need this you must update the request returned in a separate statement.
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 “application/x−www−form−urlencoded” content type. This means that you can emulate an HTML