| 1 | package HTTP::Request; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | 9 | our $VERSION = '7.04'; | |||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | 9 | ▶ | use parent 'HTTP::Message'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | sub new | ||||||||||||||||||||||||||||||||
| 11 | 54 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 12 | 54 | my($class, $method, $uri, $header, $content) = @_; | |||||||||||||||||||||||||||||||
| 13 | 54 | my $self = $class->SUPER::new($header, $content); | |||||||||||||||||||||||||||||||
| 14 | 54 | $self->method($method); | |||||||||||||||||||||||||||||||
| 15 | 54 | $self->uri($uri); | |||||||||||||||||||||||||||||||
| 16 | 52 | $self; | |||||||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | sub parse | ||||||||||||||||||||||||||||||||
| 21 | 15 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
parse: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||||||
| 22 | 15 | my($class, $str) = @_; | |||||||||||||||||||||||||||||||
| 23 | 15 | ▶ | Carp::carp('Undefined argument to parse()') if $^W && ! defined $str; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$^W and not defined $str
MC/DC100%2/2
$^W and not defined $str
$^W
not defined $str
Truth table100%3/3
A and not(B)
A= $^WB= not defined $str
| |||||||||||||||||||||||||||||||||
| 24 | 15 | my $request_line; | |||||||||||||||||||||||||||||||
| 25 | 15 | ▶ | if (defined $str && $str =~ s/^(.*)\n//) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $str and $str =~ s/^(.*)\n//
MC/DC100%2/2
defined $str and $str =~ s/^(.*)\n//
defined $str
$str =~ s/^(.*)\n//
Truth table100%3/3
A and B
A= defined $strB= $str =~ s/^(.*)\n//
| |||||||||||||||||||||||||||||||||
| 26 | 11 | $request_line = $1; | |||||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||||
| 28 | else { | ||||||||||||||||||||||||||||||||
| 29 | 4 | $request_line = $str; | |||||||||||||||||||||||||||||||
| 30 | 4 | $str = ""; | |||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||
| 33 | 15 | my $self = $class->SUPER::parse($str); | |||||||||||||||||||||||||||||||
| 34 | 15 | ▶ | if (defined $request_line) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 35 | 13 | my($method, $uri, $protocol) = split(' ', $request_line); | |||||||||||||||||||||||||||||||
| 36 | 13 | $self->method($method); | |||||||||||||||||||||||||||||||
| 37 | 13 | ▶ | $self->uri($uri) if defined($uri); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 38 | 13 | ▶ | $self->protocol($protocol) if $protocol; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||
| 40 | 15 | $self; | |||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | sub clone | ||||||||||||||||||||||||||||||||
| 45 | 1 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
clone: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 46 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||
| 47 | 1 | my $clone = bless $self->SUPER::clone, ref($self); | |||||||||||||||||||||||||||||||
| 48 | 1 | $clone->method($self->method); | |||||||||||||||||||||||||||||||
| 49 | 1 | $clone->uri($self->uri); | |||||||||||||||||||||||||||||||
| 50 | 1 | $clone; | |||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||
| 54 | sub method | ||||||||||||||||||||||||||||||||
| 55 | 184 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
method: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 56 | 184 | shift->_elem('_method', @_); | |||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||
| 60 | sub uri | ||||||||||||||||||||||||||||||||
| 61 | 133 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
uri: called CC=11 SCAR=25.6
Pod
documented
| |||||||||||||||||||||||||||||||||
| 62 | 133 | my $self = shift; | |||||||||||||||||||||||||||||||
| 63 | 133 | my $old = $self->{'_uri'}; | |||||||||||||||||||||||||||||||
| 64 | 133 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 65 | 71 | my $uri = shift; | |||||||||||||||||||||||||||||||
| 66 | 71 | ▶ | if (!defined $uri) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 67 | # that's ok | ||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||
| 69 | elsif (ref $uri) { | ||||||||||||||||||||||||||||||||
| 70 | 8 | ▶ | Carp::croak("A URI can't be a " . ref($uri) . " reference") | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $uri eq "HASH" or ref $uri eq "ARRAY"
MC/DC100%2/2
ref $uri eq "HASH" or ref $uri eq "ARRAY"
ref $uri eq "HASH"
ref $uri eq "ARRAY"
Truth table100%3/3
A or B
A= ref $uri eq "HASH"B= ref $uri eq "ARRAY"
| |||||||||||||||||||||||||||||||||
| 71 | if ref($uri) eq 'HASH' or ref($uri) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||
| 72 | 6 | ▶ | Carp::croak("Can't use a " . ref($uri) . " object as a URI") | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$uri->can("scheme") and $uri->can("canonical")
MC/DC100%2/2
$uri->can("scheme") and $uri->can("canonical")
$uri->can("scheme")
$uri->can("canonical")
Truth table100%3/3
A and B
A= $uri->can("scheme")B= $uri->can("canonical")
| |||||||||||||||||||||||||||||||||
| 73 | unless $uri->can('scheme') && $uri->can('canonical'); | ||||||||||||||||||||||||||||||||
| 74 | 4 | $uri = $uri->clone; | |||||||||||||||||||||||||||||||
| 75 | 4 | ▶ | unless ($HTTP::URI_CLASS eq "URI") { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 76 | # Argh!! Hate this... old LWP legacy! | ||||||||||||||||||||||||||||||||
| 77 | 0 | eval { local $SIG{__DIE__}; $uri = $uri->abs; }; | |||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | die $@ if $@ && $@ !~ /Missing base argument/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$@ and not $@ =~ /Missing base argument/
MC/DC0%0/2
$@ and not $@ =~ /Missing base argument/
$@
not $@ =~ /Missing base argument/
Truth table0%0/3
A and not(B)
A= $@B= not $@ =~ /Missing base argument/
| |||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||
| 81 | else { | ||||||||||||||||||||||||||||||||
| 82 | 61 | $uri = $HTTP::URI_CLASS->new($uri); | |||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||
| 84 | 67 | $self->{'_uri'} = $uri; | |||||||||||||||||||||||||||||||
| 85 | 67 | delete $self->{'_uri_canonical'}; | |||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||
| 87 | 129 | $old; | |||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||
| 90 | 9 | *url = \&uri; # legacy | |||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||
| 92 | sub uri_canonical | ||||||||||||||||||||||||||||||||
| 93 | 13 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
uri_canonical: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 94 | 13 | my $self = shift; | |||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||
| 96 | 13 | my $uri = $self->{_uri}; | |||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||
| 98 | 13 | ▶ | if (defined (my $canon = $self->{_uri_canonical})) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 99 | # early bailout if these are the exact same string; | ||||||||||||||||||||||||||||||||
| 100 | # rely on stringification of the URI objects | ||||||||||||||||||||||||||||||||
| 101 | 9 | ▶ | return $canon if $canon eq $uri; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||
| 104 | # otherwise we need to refresh the memoized value | ||||||||||||||||||||||||||||||||
| 105 | 4 | $self->{_uri_canonical} = $uri->canonical; | |||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||
| 109 | sub accept_decodable | ||||||||||||||||||||||||||||||||
| 110 | 1 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
accept_decodable: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 111 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||
| 112 | 1 | $self->header("Accept-Encoding", scalar($self->decodable)); | |||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||
| 115 | sub as_string | ||||||||||||||||||||||||||||||||
| 116 | 25 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
as_string: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 117 | 25 | my $self = shift; | |||||||||||||||||||||||||||||||
| 118 | 25 | my($eol) = @_; | |||||||||||||||||||||||||||||||
| 119 | 25 | ▶ | $eol = "\n" unless defined $eol; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||
| 121 | # method must be at least one char, matching ^[a-zA-Z0-9!#$%&'*+.^_`|~-]+$ | ||||||||||||||||||||||||||||||||
| 122 | 25 | ▶ | my $req_line = (defined $self->method && length $self->method) ? $self->method : "-"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->method and length $self->method
MC/DC50%1/2
defined $self->method and length $self->method
defined $self->method
length $self->method
Truth table66%2/3
A and B
A= defined $self->methodB= length $self->method
| |||||||||||||||||||||||||||||||||
| 123 | 25 | my $uri = $self->uri; | |||||||||||||||||||||||||||||||
| 124 | 25 | ▶ | $uri = (defined $uri) ? $uri->as_string : "-"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 125 | 25 | $req_line .= " $uri"; | |||||||||||||||||||||||||||||||
| 126 | 25 | my $proto = $self->protocol; | |||||||||||||||||||||||||||||||
| 127 | 25 | ▶ | $req_line .= " $proto" if $proto; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||
| 129 | 25 | return join($eol, $req_line, $self->SUPER::as_string($eol)); | |||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||
| 132 | sub dump | ||||||||||||||||||||||||||||||||
| 133 | 4 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
dump: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 134 | 4 | my $self = shift; | |||||||||||||||||||||||||||||||
| 135 | 4 | ▶ | my @pre = ((defined $self->method && length $self->method) ? $self->method : "-", (defined $self->uri) ? $self->uri : "-"); | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
defined $self->method and length $self->method
MC/DC50%1/2
defined $self->method and length $self->method
defined $self->method
length $self->method
Truth table66%2/3
A and B
A= defined $self->methodB= length $self->method
| |||||||||||||||||||||||||||||||||
| 136 | 4 | ▶ | if (my $prot = $self->protocol) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 137 | 1 | push(@pre, $prot); | |||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||
| 140 | 4 | return $self->SUPER::dump( | |||||||||||||||||||||||||||||||
| 141 | preheader => join(" ", @pre), | ||||||||||||||||||||||||||||||||
| 142 | @_, | ||||||||||||||||||||||||||||||||
| 143 | ); | ||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||
| 147 | 9 | 1; | |||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||
| 149 | =pod | ||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||
| 151 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||
| 153 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||
| 155 | HTTP::Request - HTTP style request message | ||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||
| 157 | =head1 VERSION | ||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||
| 159 | version 7.04 | ||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||
| 161 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||
| 163 | require HTTP::Request; | ||||||||||||||||||||||||||||||||
| 164 | $request = HTTP::Request->new(GET => 'http://www.example.com/'); | ||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||
| 166 | and usually used like this: | ||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||
| 168 | $ua = LWP::UserAgent->new; | ||||||||||||||||||||||||||||||||
| 169 | $response = $ua->request($request); | ||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||
| 171 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||
| 173 | C<HTTP::Request> is a class encapsulating HTTP style requests, | ||||||||||||||||||||||||||||||||
| 174 | consisting of a request line, some headers, and a content body. Note | ||||||||||||||||||||||||||||||||
| 175 | that the LWP library uses HTTP style requests even for non-HTTP | ||||||||||||||||||||||||||||||||
| 176 | protocols. Instances of this class are usually passed to the | ||||||||||||||||||||||||||||||||
| 177 | request() method of an C<LWP::UserAgent> object. | ||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||
| 179 | C<HTTP::Request> is a subclass of C<HTTP::Message> and therefore | ||||||||||||||||||||||||||||||||
| 180 | inherits its methods. The following additional methods are available: | ||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||
| 182 | =over 4 | ||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||
| 184 | =item $r = HTTP::Request->new( $method, $uri ) | ||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||
| 186 | =item $r = HTTP::Request->new( $method, $uri, $header ) | ||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||
| 188 | =item $r = HTTP::Request->new( $method, $uri, $header, $content ) | ||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||
| 190 | Constructs a new C<HTTP::Request> object describing a request on the | ||||||||||||||||||||||||||||||||
| 191 | object $uri using method $method. The $method argument must be a | ||||||||||||||||||||||||||||||||
| 192 | string. The $uri argument can be either a string, or a reference to a | ||||||||||||||||||||||||||||||||
| 193 | C<URI> object. The optional $header argument should be a reference to | ||||||||||||||||||||||||||||||||
| 194 | an C<HTTP::Headers> object or a plain array reference of key/value | ||||||||||||||||||||||||||||||||
| 195 | pairs. The optional $content argument should be a string of bytes. | ||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||
| 197 | =item $r = HTTP::Request->parse( $str ) | ||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||
| 199 | This constructs a new request object by parsing the given string. | ||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||
| 201 | =item $r->method | ||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||
| 203 | =item $r->method( $val ) | ||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||
| 205 | This is used to get/set the method attribute. The method should be a | ||||||||||||||||||||||||||||||||
| 206 | short string like "DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", | ||||||||||||||||||||||||||||||||
| 207 | "PUT" or "QUERY". | ||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||
| 209 | =item $r->uri | ||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||
| 211 | =item $r->uri( $val ) | ||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||
| 213 | This is used to get/set the uri attribute. The $val can be a | ||||||||||||||||||||||||||||||||
| 214 | reference to a URI object or a plain string. If a string is given, | ||||||||||||||||||||||||||||||||
| 215 | then it should be parsable as an absolute URI. | ||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||
| 217 | =item $r->header( $field ) | ||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||
| 219 | =item $r->header( $field => $value ) | ||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||
| 221 | This is used to get/set header values and it is inherited from | ||||||||||||||||||||||||||||||||
| 222 | C<HTTP::Headers> via C<HTTP::Message>. See L<HTTP::Headers> for | ||||||||||||||||||||||||||||||||
| 223 | details and other similar methods that can be used to access the | ||||||||||||||||||||||||||||||||
| 224 | headers. | ||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||
| 226 | =item $r->accept_decodable | ||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||
| 228 | This will set the C<Accept-Encoding> header to the list of encodings | ||||||||||||||||||||||||||||||||
| 229 | that decoded_content() can decode. | ||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||
| 231 | =item $r->content | ||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||
| 233 | =item $r->content( $bytes ) | ||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||
| 235 | This is used to get/set the content and it is inherited from the | ||||||||||||||||||||||||||||||||
| 236 | C<HTTP::Message> base class. See L<HTTP::Message> for details and | ||||||||||||||||||||||||||||||||
| 237 | other methods that can be used to access the content. | ||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||
| 239 | Note that the content should be a string of bytes. Strings in perl | ||||||||||||||||||||||||||||||||
| 240 | can contain characters outside the range of a byte. The C<Encode> | ||||||||||||||||||||||||||||||||
| 241 | module can be used to turn such strings into a string of bytes. | ||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||
| 243 | =item $r->as_string | ||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||
| 245 | =item $r->as_string( $eol ) | ||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||
| 247 | Method returning a textual representation of the request. | ||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||
| 249 | =back | ||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||
| 251 | =head1 EXAMPLES | ||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||
| 253 | Creating requests to be sent with L<LWP::UserAgent> or others can be easy. Here | ||||||||||||||||||||||||||||||||
| 254 | are a few examples. | ||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||
| 256 | =head2 Simple POST | ||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||
| 258 | Here, we'll create a simple POST request that could be used to send JSON data | ||||||||||||||||||||||||||||||||
| 259 | to an endpoint. | ||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||
| 261 | #!/usr/bin/env perl | ||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||
| 263 | use strict; | ||||||||||||||||||||||||||||||||
| 264 | use warnings; | ||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||
| 266 | use HTTP::Request (); | ||||||||||||||||||||||||||||||||
| 267 | use JSON::MaybeXS qw(encode_json); | ||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||
| 269 | my $url = 'https://www.example.com/api/user/123'; | ||||||||||||||||||||||||||||||||
| 270 | my $header = ['Content-Type' => 'application/json; charset=UTF-8']; | ||||||||||||||||||||||||||||||||
| 271 | my $data = {foo => 'bar', baz => 'quux'}; | ||||||||||||||||||||||||||||||||
| 272 | my $encoded_data = encode_json($data); | ||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||
| 274 | my $r = HTTP::Request->new('POST', $url, $header, $encoded_data); | ||||||||||||||||||||||||||||||||
| 275 | # at this point, we could send it via LWP::UserAgent | ||||||||||||||||||||||||||||||||
| 276 | # my $ua = LWP::UserAgent->new(); | ||||||||||||||||||||||||||||||||
| 277 | # my $res = $ua->request($r); | ||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||
| 279 | =head2 Batch POST Request | ||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||
| 281 | Some services, like Google, allow multiple requests to be sent in one batch. | ||||||||||||||||||||||||||||||||
| 282 | L<https://developers.google.com/drive/v3/web/batch> for example. Using the | ||||||||||||||||||||||||||||||||
| 283 | C<add_part> method from L<HTTP::Message> makes this simple. | ||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||
| 285 | #!/usr/bin/env perl | ||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||
| 287 | use strict; | ||||||||||||||||||||||||||||||||
| 288 | use warnings; | ||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||
| 290 | use HTTP::Request (); | ||||||||||||||||||||||||||||||||
| 291 | use JSON::MaybeXS qw(encode_json); | ||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||
| 293 | my $auth_token = 'auth_token'; | ||||||||||||||||||||||||||||||||
| 294 | my $batch_url = 'https://www.googleapis.com/batch'; | ||||||||||||||||||||||||||||||||
| 295 | my $url = 'https://www.googleapis.com/drive/v3/files/fileId/permissions?fields=id'; | ||||||||||||||||||||||||||||||||
| 296 | my $url_no_email = 'https://www.googleapis.com/drive/v3/files/fileId/permissions?fields=id&sendNotificationEmail=false'; | ||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||
| 298 | # generate a JSON post request for one of the batch entries | ||||||||||||||||||||||||||||||||
| 299 | my $req1 = build_json_request($url, { | ||||||||||||||||||||||||||||||||
| 300 | emailAddress => 'example@appsrocks.com', | ||||||||||||||||||||||||||||||||
| 301 | role => "writer", | ||||||||||||||||||||||||||||||||
| 302 | type => "user", | ||||||||||||||||||||||||||||||||
| 303 | }); | ||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||
| 305 | # generate a JSON post request for one of the batch entries | ||||||||||||||||||||||||||||||||
| 306 | my $req2 = build_json_request($url_no_email, { | ||||||||||||||||||||||||||||||||
| 307 | domain => "appsrocks.com", | ||||||||||||||||||||||||||||||||
| 308 | role => "reader", | ||||||||||||||||||||||||||||||||
| 309 | type => "domain", | ||||||||||||||||||||||||||||||||
| 310 | }); | ||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||
| 312 | # generate a multipart request to send all of the other requests | ||||||||||||||||||||||||||||||||
| 313 | my $r = HTTP::Request->new('POST', $batch_url, [ | ||||||||||||||||||||||||||||||||
| 314 | 'Accept-Encoding' => 'gzip', | ||||||||||||||||||||||||||||||||
| 315 | # if we don't provide a boundary here, HTTP::Message will generate | ||||||||||||||||||||||||||||||||
| 316 | # one for us. We could use UUID::uuid() here if we wanted. | ||||||||||||||||||||||||||||||||
| 317 | 'Content-Type' => 'multipart/mixed; boundary=END_OF_PART' | ||||||||||||||||||||||||||||||||
| 318 | ]); | ||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||
| 320 | # add the two POST requests to the main request | ||||||||||||||||||||||||||||||||
| 321 | $r->add_part($req1, $req2); | ||||||||||||||||||||||||||||||||
| 322 | # at this point, we could send it via LWP::UserAgent | ||||||||||||||||||||||||||||||||
| 323 | # my $ua = LWP::UserAgent->new(); | ||||||||||||||||||||||||||||||||
| 324 | # my $res = $ua->request($r); | ||||||||||||||||||||||||||||||||
| 325 | exit(); | ||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||
| 327 | sub build_json_request { | ||||||||||||||||||||||||||||||||
| 328 | my ($url, $href) = @_; | ||||||||||||||||||||||||||||||||
| 329 | my $header = ['Authorization' => "Bearer $auth_token", 'Content-Type' => 'application/json; charset=UTF-8']; | ||||||||||||||||||||||||||||||||
| 330 | return HTTP::Request->new('POST', $url, $header, encode_json($href)); | ||||||||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||
| 333 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||
| 335 | L<HTTP::Headers>, L<HTTP::Message>, L<HTTP::Request::Common>, | ||||||||||||||||||||||||||||||||
| 336 | L<HTTP::Response> | ||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||
| 338 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||
| 340 | Gisle Aas <gisle@activestate.com> | ||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||
| 342 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||
| 344 | This software is copyright (c) 1994 by Gisle Aas. | ||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||
| 346 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||
| 347 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||
| 349 | =cut | ||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||
| 351 | __END__ | ||||||||||||||||||||||||||||||||