[HOME]

Path : /proc/self/root/usr/local/share/man/man3/
Upload :
Current File : //proc/self/root/usr/local/share/man/man3/HTTP::Request.3pm

.\" Automatically generated by Pod::Man 2.27 (Pod::Simple 3.28)
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" Set up some character translations and predefined strings.  \*(-- will
.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
.\" double quote, and \*(R" will give a right double quote.  \*(C+ will
.\" give a nicer C++.  Capital omega is used to do unbreakable dashes and
.\" therefore won't be available.  \*(C` and \*(C' expand to `' in nroff,
.\" nothing in troff, for use with C<>.
.tr \(*W-
.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
.ie n \{\
.    ds -- \(*W-
.    ds PI pi
.    if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
.    if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\"  diablo 12 pitch
.    ds L" ""
.    ds R" ""
.    ds C` ""
.    ds C' ""
'br\}
.el\{\
.    ds -- \|\(em\|
.    ds PI \(*p
.    ds L" ``
.    ds R" ''
.    ds C`
.    ds C'
'br\}
.\"
.\" Escape single quotes in literal strings from groff's Unicode transform.
.ie \n(.g .ds Aq \(aq
.el       .ds Aq '
.\"
.\" If the F register is turned on, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
.\" entries marked with X<> in POD.  Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.\"
.\" Avoid warning from groff about undefined register 'F'.
.de IX
..
.nr rF 0
.if \n(.g .if rF .nr rF 1
.if (\n(rF:(\n(.g==0)) \{
.    if \nF \{
.        de IX
.        tm Index:\\$1\t\\n%\t"\\$2"
..
.        if !\nF==2 \{
.            nr % 0
.            nr F 2
.        \}
.    \}
.\}
.rr rF
.\" ========================================================================
.\"
.IX Title "HTTP::Request 3"
.TH HTTP::Request 3 "2018-06-05" "perl v5.16.3" "User Contributed Perl Documentation"
.\" For nroff, turn off justification.  Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
.nh
.SH "NAME"
HTTP::Request \- HTTP style request message
.SH "VERSION"
.IX Header "VERSION"
version 6.18
.SH "SYNOPSIS"
.IX Header "SYNOPSIS"
.Vb 2
\& require HTTP::Request;
\& $request = HTTP::Request\->new(GET => \*(Aqhttp://www.example.com/\*(Aq);
.Ve
.PP
and usually used like this:
.PP
.Vb 2
\& $ua = LWP::UserAgent\->new;
\& $response = $ua\->request($request);
.Ve
.SH "DESCRIPTION"
.IX Header "DESCRIPTION"
\&\f(CW\*(C`HTTP::Request\*(C'\fR is a class encapsulating \s-1HTTP\s0 style requests,
consisting of a request line, some headers, and a content body. Note
that the \s-1LWP\s0 library uses \s-1HTTP\s0 style requests even for non-HTTP
protocols.  Instances of this class are usually passed to the
\&\fIrequest()\fR method of an \f(CW\*(C`LWP::UserAgent\*(C'\fR object.
.PP
\&\f(CW\*(C`HTTP::Request\*(C'\fR is a subclass of \f(CW\*(C`HTTP::Message\*(C'\fR and therefore
inherits its methods.  The following additional methods are available:
.ie n .IP "$r = HTTP::Request\->new( $method, $uri )" 4
.el .IP "\f(CW$r\fR = HTTP::Request\->new( \f(CW$method\fR, \f(CW$uri\fR )" 4
.IX Item "$r = HTTP::Request->new( $method, $uri )"
.PD 0
.ie n .IP "$r = HTTP::Request\->new( $method, $uri, $header )" 4
.el .IP "\f(CW$r\fR = HTTP::Request\->new( \f(CW$method\fR, \f(CW$uri\fR, \f(CW$header\fR )" 4
.IX Item "$r = HTTP::Request->new( $method, $uri, $header )"
.ie n .IP "$r = HTTP::Request\->new( $method, $uri, $header, $content )" 4
.el .IP "\f(CW$r\fR = HTTP::Request\->new( \f(CW$method\fR, \f(CW$uri\fR, \f(CW$header\fR, \f(CW$content\fR )" 4
.IX Item "$r = HTTP::Request->new( $method, $uri, $header, $content )"
.PD
Constructs a new \f(CW\*(C`HTTP::Request\*(C'\fR object describing a request on the
object \f(CW$uri\fR using method \f(CW$method\fR.  The \f(CW$method\fR argument must be a
string.  The \f(CW$uri\fR argument can be either a string, or a reference to a
\&\f(CW\*(C`URI\*(C'\fR object.  The optional \f(CW$header\fR argument should be a reference to
an \f(CW\*(C`HTTP::Headers\*(C'\fR object or a plain array reference of key/value
pairs.  The optional \f(CW$content\fR argument should be a string of bytes.
.ie n .IP "$r = HTTP::Request\->parse( $str )" 4
.el .IP "\f(CW$r\fR = HTTP::Request\->parse( \f(CW$str\fR )" 4
.IX Item "$r = HTTP::Request->parse( $str )"
This constructs a new request object by parsing the given string.
.ie n .IP "$r\->method" 4
.el .IP "\f(CW$r\fR\->method" 4
.IX Item "$r->method"
.PD 0
.ie n .IP "$r\->method( $val )" 4
.el .IP "\f(CW$r\fR\->method( \f(CW$val\fR )" 4
.IX Item "$r->method( $val )"
.PD
This is used to get/set the method attribute.  The method should be a
short string like \*(L"\s-1GET\*(R", \*(L"HEAD\*(R", \*(L"PUT\*(R", \*(L"PATCH\*(R"\s0 or \*(L"\s-1POST\*(R".\s0
.ie n .IP "$r\->uri" 4
.el .IP "\f(CW$r\fR\->uri" 4
.IX Item "$r->uri"
.PD 0
.ie n .IP "$r\->uri( $val )" 4
.el .IP "\f(CW$r\fR\->uri( \f(CW$val\fR )" 4
.IX Item "$r->uri( $val )"
.PD
This is used to get/set the uri attribute.  The \f(CW$val\fR can be a
reference to a \s-1URI\s0 object or a plain string.  If a string is given,
then it should be parsable as an absolute \s-1URI.\s0
.ie n .IP "$r\->header( $field )" 4
.el .IP "\f(CW$r\fR\->header( \f(CW$field\fR )" 4
.IX Item "$r->header( $field )"
.PD 0
.ie n .IP "$r\->header( $field => $value )" 4
.el .IP "\f(CW$r\fR\->header( \f(CW$field\fR => \f(CW$value\fR )" 4
.IX Item "$r->header( $field => $value )"
.PD
This is used to get/set header values and it is inherited from
\&\f(CW\*(C`HTTP::Headers\*(C'\fR via \f(CW\*(C`HTTP::Message\*(C'\fR.  See HTTP::Headers for
details and other similar methods that can be used to access the
headers.
.ie n .IP "$r\->accept_decodable" 4
.el .IP "\f(CW$r\fR\->accept_decodable" 4
.IX Item "$r->accept_decodable"
This will set the \f(CW\*(C`Accept\-Encoding\*(C'\fR header to the list of encodings
that \fIdecoded_content()\fR can decode.
.ie n .IP "$r\->content" 4
.el .IP "\f(CW$r\fR\->content" 4
.IX Item "$r->content"
.PD 0
.ie n .IP "$r\->content( $bytes )" 4
.el .IP "\f(CW$r\fR\->content( \f(CW$bytes\fR )" 4
.IX Item "$r->content( $bytes )"
.PD
This is used to get/set the content and it is inherited from the
\&\f(CW\*(C`HTTP::Message\*(C'\fR base class.  See HTTP::Message for details and
other methods that can be used to access the content.
.Sp
Note that the content should be a string of bytes.  Strings in perl
can contain characters outside the range of a byte.  The \f(CW\*(C`Encode\*(C'\fR
module can be used to turn such strings into a string of bytes.
.ie n .IP "$r\->as_string" 4
.el .IP "\f(CW$r\fR\->as_string" 4
.IX Item "$r->as_string"
.PD 0
.ie n .IP "$r\->as_string( $eol )" 4
.el .IP "\f(CW$r\fR\->as_string( \f(CW$eol\fR )" 4
.IX Item "$r->as_string( $eol )"
.PD
Method returning a textual representation of the request.
.SH "EXAMPLES"
.IX Header "EXAMPLES"
Creating requests to be sent with LWP::UserAgent or others can be easy. Here
are a few examples.
.SS "Simple \s-1POST\s0"
.IX Subsection "Simple POST"
Here, we'll create a simple \s-1POST\s0 request that could be used to send \s-1JSON\s0 data
to an endpoint.
.PP
.Vb 1
\&    #!/usr/bin/env perl
\&
\&    use strict;
\&    use warnings;
\&
\&    use Encode qw(encode_utf8);
\&    use HTTP::Request ();
\&    use JSON::MaybeXS qw(encode_json);
\&
\&    my $url = \*(Aqhttps://www.example.com/api/user/123\*(Aq;
\&    my $header = [\*(AqContent\-Type\*(Aq => \*(Aqapplication/json; charset=UTF\-8\*(Aq];
\&    my $data = {foo => \*(Aqbar\*(Aq, baz => \*(Aqquux\*(Aq};
\&    my $encoded_data = encode_utf8(encode_json($data));
\&
\&    my $r = HTTP::Request\->new(\*(AqPOST\*(Aq, $url, $header, $encoded_data);
\&    # at this point, we could send it via LWP::UserAgent
\&    # my $ua = LWP::UserAgent\->new();
\&    # my $res = $ua\->request($r);
.Ve
.SS "Batch \s-1POST\s0 Request"
.IX Subsection "Batch POST Request"
Some services, like Google, allow multiple requests to be sent in one batch.
<https://developers.google.com/drive/v3/web/batch> for example. Using the
\&\f(CW\*(C`add_part\*(C'\fR method from HTTP::Message makes this simple.
.PP
.Vb 1
\&    #!/usr/bin/env perl
\&
\&    use strict;
\&    use warnings;
\&
\&    use Encode qw(encode_utf8);
\&    use HTTP::Request ();
\&    use JSON::MaybeXS qw(encode_json);
\&
\&    my $auth_token = \*(Aqauth_token\*(Aq;
\&    my $batch_url = \*(Aqhttps://www.googleapis.com/batch\*(Aq;
\&    my $url = \*(Aqhttps://www.googleapis.com/drive/v3/files/fileId/permissions?fields=id\*(Aq;
\&    my $url_no_email = \*(Aqhttps://www.googleapis.com/drive/v3/files/fileId/permissions?fields=id&sendNotificationEmail=false\*(Aq;
\&
\&    # generate a JSON post request for one of the batch entries
\&    my $req1 = build_json_request($url, {
\&        emailAddress => \*(Aqexample@appsrocks.com\*(Aq,
\&        role => "writer",
\&        type => "user",
\&    });
\&
\&    # generate a JSON post request for one of the batch entries
\&    my $req2 = build_json_request($url_no_email, {
\&        domain => "appsrocks.com",
\&        role => "reader",
\&        type => "domain",
\&    });
\&
\&    # generate a multipart request to send all of the other requests
\&    my $r = HTTP::Request\->new(\*(AqPOST\*(Aq, $batch_url, [
\&        \*(AqAccept\-Encoding\*(Aq => \*(Aqgzip\*(Aq,
\&        # if we don\*(Aqt provide a boundary here, HTTP::Message will generate
\&        # one for us. We could use UUID::uuid() here if we wanted.
\&        \*(AqContent\-Type\*(Aq => \*(Aqmultipart/mixed; boundary=END_OF_PART\*(Aq
\&    ]);
\&
\&    # add the two POST requests to the main request
\&    $r\->add_part($req1, $req2);
\&    # at this point, we could send it via LWP::UserAgent
\&    # my $ua = LWP::UserAgent\->new();
\&    # my $res = $ua\->request($r);
\&    exit();
\&
\&    sub build_json_request {
\&        my ($url, $href) = @_;
\&        my $header = [\*(AqAuthorization\*(Aq => "Bearer $auth_token", \*(AqContent\-Type\*(Aq => \*(Aqapplication/json; charset=UTF\-8\*(Aq];
\&        return HTTP::Request\->new(\*(AqPOST\*(Aq, $url, $header, encode_utf8(encode_json($href)));
\&    }
.Ve
.SH "SEE ALSO"
.IX Header "SEE ALSO"
HTTP::Headers, HTTP::Message, HTTP::Request::Common,
HTTP::Response
.SH "AUTHOR"
.IX Header "AUTHOR"
Gisle Aas <gisle@activestate.com>
.SH "COPYRIGHT AND LICENSE"
.IX Header "COPYRIGHT AND LICENSE"
This software is copyright (c) 1994\-2017 by Gisle Aas.
.PP
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.