22package HTTP::Tiny ;
33use strict;
44use warnings;
5+ # ABSTRACT: A small, simple, correct HTTP/1.1 client
56# VERSION
67
78use Carp ();
9798
9899This method executes a C<POST > request and sends the key/value pairs from a
99100form data hash or array reference to the given URL with a C<content-type > of
100- C<application/x-www-form-urlencoded > . The keys and values posted to the form
101- will be UTF-8 encoded and escaped per RFC 3986. If a value is an array
102- reference, the key will be repeated with each of the values of the array
103- reference.
101+ C<application/x-www-form-urlencoded > . See documentation for the
102+ C<www_form_urlencode > method for details on the encoding.
104103
105104The URL must have unsafe characters escaped and international domain names
106105encoded. See C<request() > for valid options and a description of the response.
107- Any <content-type> header or content in the options hashref will be ignored.
106+ Any C <content-type > header or content in the options hashref will be ignored.
108107
109108=cut
110109
111110sub post_form {
112111 my ($self , $url , $data , $args ) = @_ ;
113112 (@_ == 3 || @_ == 4 && ref $args eq ' HASH' )
114113 or Carp::croak(q/ Usage: $http->post_form(URL, DATAREF, [HASHREF])/ . " \n " );
115- ref $data eq ' HASH' || ref $data eq ' ARRAY'
116- or Carp::croak(" post_form() data reference must be hashref or arrayref\n " );
117-
118- my @params = ref $data eq ' HASH' ? %$data : @$data ;
119- @params % 2 == 0
120- or Carp::croak(" post_form() data arrayref must have an even number of terms\n " );
121-
122- my @terms ;
123- while ( @params ) {
124- my ($key , $value ) = splice (@params , 0, 2);
125- if ( ref $value eq ' ARRAY' ) {
126- unshift @params , map { $key => $_ } @$value ;
127- }
128- else {
129- push @terms , join (" =" , map { $self -> _uri_escape($_ ) } $key , $value );
130- }
131- }
132114
133115 my $headers = {};
134116 while ( my ($key , $value ) = each %{$args -> {headers } || {}} ) {
@@ -138,7 +120,7 @@ sub post_form {
138120
139121 return $self -> request(' POST' , $url , {
140122 %$args ,
141- content => join ( " & " , sort @terms ),
123+ content => $self -> www_form_urlencode( $data ),
142124 headers => {
143125 %$headers ,
144126 ' content-type' => ' application/x-www-form-urlencoded'
@@ -291,6 +273,49 @@ sub request {
291273 return $response ;
292274}
293275
276+ =method www_form_urlencode
277+
278+ $params = $http->www_form_urlencode( $data );
279+ $response = $http->get("http://example.com/query?$params");
280+
281+ This method converts the key/value pairs from a data hash or array reference
282+ into a C<x-www-form-urlencoded > string. The keys and values from the data
283+ reference will be UTF-8 encoded and escaped per RFC 3986. If a value is an
284+ array reference, the key will be repeated with each of the values of the array
285+ reference. The key/value pairs in the resulting string will be sorted by key
286+ and value.
287+
288+ =cut
289+
290+ sub www_form_urlencode {
291+ my ($self , $data ) = @_ ;
292+ (@_ == 2 && ref $data )
293+ or Carp::croak(q/ Usage: $http->www_form_urlencode(DATAREF)/ . " \n " );
294+ (ref $data eq ' HASH' || ref $data eq ' ARRAY' )
295+ or Carp::croak(" form data must be a hash or array reference" );
296+
297+ my @params = ref $data eq ' HASH' ? %$data : @$data ;
298+ @params % 2 == 0
299+ or Carp::croak(" form data reference must have an even number of terms\n " );
300+
301+ my @terms ;
302+ while ( @params ) {
303+ my ($key , $value ) = splice (@params , 0, 2);
304+ if ( ref $value eq ' ARRAY' ) {
305+ unshift @params , map { $key => $_ } @$value ;
306+ }
307+ else {
308+ push @terms , join (" =" , map { $self -> _uri_escape($_ ) } $key , $value );
309+ }
310+ }
311+
312+ return join (" &" , sort @terms );
313+ }
314+
315+ # --------------------------------------------------------------------------#
316+ # private methods
317+ # --------------------------------------------------------------------------#
318+
294319my %DefaultPort = (
295320 http => 80,
296321 https => 443,
@@ -940,8 +965,6 @@ sub can_write {
940965
9419661;
942967
943- # ABSTRACT: A small, simple, correct HTTP/1.1 client
944-
945968=for Pod::Coverage
946969agent
947970default_headers
@@ -970,7 +993,7 @@ timeout
970993
971994=head1 DESCRIPTION
972995
973- This is a very simple HTTP/1.1 client, designed primarily for doing simple GET
996+ This is a very simple HTTP/1.1 client, designed for doing simple GET
974997requests without the overhead of a large framework like L<LWP::UserAgent> .
975998
976999It is more correct and more complete than L<HTTP::Lite> . It supports
0 commit comments