Skip to content
This repository was archived by the owner on Sep 28, 2022. It is now read-only.

Commit 46182db

Browse files
committed
Add www_form_urlencode method
1 parent 22312ba commit 46182db

File tree

4 files changed

+70
-33
lines changed

4 files changed

+70
-33
lines changed

Changes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Release notes for HTTP-Tiny
1010
- post_form() method for POST-ing x-www-form-urlencoded data
1111
[David Golden]
1212

13+
- www_form_urlencode() utility method [David Golden]
14+
1315
0.013 2011-07-17 23:14:22 America/New_York
1416

1517
[NEW FEATURES]

README.pod

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ version 0.014
2626

2727
=head1 DESCRIPTION
2828

29-
This is a very simple HTTP/1.1 client, designed primarily for doing simple GET
29+
This is a very simple HTTP/1.1 client, designed for doing simple GET
3030
requests without the overhead of a large framework like L<LWP::UserAgent>.
3131

3232
It is more correct and more complete than L<HTTP::Lite>. It supports
@@ -99,14 +99,12 @@ See C<request()> for valid options and a description of the response.
9999

100100
This method executes a C<POST> request and sends the key/value pairs from a
101101
form data hash or array reference to the given URL with a C<content-type> of
102-
C<application/x-www-form-urlencoded>. The keys and values posted to the form
103-
will be UTF-8 encoded and escaped per RFC 3986. If a value is an array
104-
reference, the key will be repeated with each of the values of the array
105-
reference.
102+
C<application/x-www-form-urlencoded>. See documentation for the
103+
C<www_form_urlencode> method for details on the encoding.
106104

107105
The URL must have unsafe characters escaped and international domain names
108106
encoded. See C<request()> for valid options and a description of the response.
109-
Any <content-type> header or content in the options hashref will be ignored.
107+
Any C<content-type> header or content in the options hashref will be ignored.
110108

111109
=head2 mirror
112110

@@ -229,6 +227,18 @@ it will otherwise be a scalar string containing the value
229227
On an exception during the execution of the request, the C<status> field will
230228
contain 599, and the C<content> field will contain the text of the exception.
231229

230+
=head2 www_form_urlencode
231+
232+
$params = $http->www_form_urlencode( $data );
233+
$response = $http->get("http://example.com/query?$params");
234+
235+
This method converts the key/value pairs from a data hash or array reference
236+
into a C<x-www-form-urlencoded> string. The keys and values from the data
237+
reference will be UTF-8 encoded and escaped per RFC 3986. If a value is an
238+
array reference, the key will be repeated with each of the values of the array
239+
reference. The key/value pairs in the resulting string will be sorted by key
240+
and value.
241+
232242
=for Pod::Coverage agent
233243
default_headers
234244
max_redirect

lib/HTTP/Tiny.pm

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
package HTTP::Tiny;
33
use strict;
44
use warnings;
5+
# ABSTRACT: A small, simple, correct HTTP/1.1 client
56
# VERSION
67

78
use Carp ();
@@ -97,38 +98,19 @@ HERE
9798
9899
This method executes a C<POST> request and sends the key/value pairs from a
99100
form 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
105104
The URL must have unsafe characters escaped and international domain names
106105
encoded. 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

111110
sub 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+
294319
my %DefaultPort = (
295320
http => 80,
296321
https => 443,
@@ -940,8 +965,6 @@ sub can_write {
940965

941966
1;
942967

943-
# ABSTRACT: A small, simple, correct HTTP/1.1 client
944-
945968
=for Pod::Coverage
946969
agent
947970
default_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
974997
requests without the overhead of a large framework like L<LWP::UserAgent>.
975998
976999
It is more correct and more complete than L<HTTP::Lite>. It supports

t/001_api.t

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ use Test::More tests => 2;
77
use HTTP::Tiny;
88

99
my @accessors = qw(agent default_headers max_redirect max_size proxy timeout);
10-
my @methods = qw(new get head put post delete post_form request mirror);
10+
my @methods = qw(
11+
new get head put post delete post_form request mirror www_form_urlencode
12+
);
1113

1214
my %api;
1315
@api{@accessors} = (1) x @accessors;

0 commit comments

Comments
 (0)