Skip to content

Commit 154535c

Browse files
committed
Merge pull request #9 from mschilli/master
Both POST() and PUT() take data in the same way
2 parents a9ef7d6 + 08c7f86 commit 154535c

3 files changed

Lines changed: 18 additions & 7 deletions

File tree

lib/HTTP/Request/Common.pm

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,20 @@ my $CRLF = "\015\012"; # "\r\n" is not portable
1919

2020
sub GET { _simple_req('GET', @_); }
2121
sub HEAD { _simple_req('HEAD', @_); }
22-
sub PUT { _simple_req('PUT' , @_); }
2322
sub DELETE { _simple_req('DELETE', @_); }
2423

25-
sub POST
24+
for my $type (qw(PUT POST)) {
25+
no strict 'refs';
26+
*{ __PACKAGE__ . "::" . $type } = sub {
27+
return request_type_with_data($type, @_);
28+
};
29+
}
30+
31+
sub request_type_with_data
2632
{
27-
my $url = shift;
28-
my $req = HTTP::Request->new(POST => $url);
33+
my $type = shift;
34+
my $url = shift;
35+
my $req = HTTP::Request->new($type => $url);
2936
my $content;
3037
$content = shift if @_ and ref $_[0];
3138
my($k, $v);

t/common-req.t

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#perl -w
22

33
use Test;
4-
plan tests => 57;
4+
plan tests => 58;
55

66
use HTTP::Request::Common;
77

@@ -35,6 +35,10 @@ ok(${$r->content_ref}, "foo");
3535
ok($r->content, "foo");
3636
ok($r->content_length, 3);
3737

38+
$r = PUT "http://www.sn.no",
39+
{ foo => "bar" };
40+
ok($r->content, "foo=bar");
41+
3842
#--- Test POST requests ---
3943

4044
$r = POST "http://www.sn.no", [foo => 'bar;baz',
@@ -226,4 +230,4 @@ $r = HTTP::Request::Common::PUT 'http://www.example.com',
226230
'Content-Type' => 'application/octet-steam',
227231
'Content' => 'foobarbaz',
228232
'Content-Length' => 12; # a slight lie
229-
ok($r->header('Content-Length'), 12);
233+
ok($r->header('Content-Length'), 9);

t/headers.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use strict;
44
use Test qw(plan ok);
55

6-
plan tests => 166;
6+
plan tests => 175;
77

88
my($h, $h2);
99
sub j { join("|", @_) }

0 commit comments

Comments
 (0)