File Coverage

blib/lib/LWP/Simple/Post.pm
Criterion Covered Total %
statement 16 30 53.3
branch 0 10 0.0
condition 0 6 0.0
subroutine 6 8 75.0
pod 2 2 100.0
total 24 56 42.8


line stmt bran cond sub pod time code
1             package LWP::Simple::Post;
2             BEGIN {
3 1     1   984 $LWP::Simple::Post::VERSION = '0.05';
4             }
5              
6 1     1   9 use strict;
  1         4  
  1         36  
7 1     1   6 use warnings;
  1         2  
  1         50  
8              
9 1     1   977 use parent "Exporter";
  1         383  
  1         5  
10             our @EXPORT_OK = qw( post post_xml );
11              
12 1     1   833412 use LWP::UserAgent;
  1         3231454  
  1         46  
13 1     1   15 use HTTP::Request;
  1         2  
  1         264  
14              
15             =head1 NAME
16              
17             LWP::Simple::Post - Single-method POST requests
18              
19             =head1 VERSION
20              
21             version 0.05
22              
23             =head1 DESCRIPTION
24              
25             Really simple wrapper to HTTP POST requests
26              
27             =head1 SYNOPSIS
28              
29             use LWP::Simple::Post qw(post post_xml);
30              
31             my $response = post('http://production/receiver', 'some text');
32              
33             =head1 OVERVIEW
34              
35             B
36             this module would be a win over using LWP::UserAgent directly.
37             It was a bad idea I implemented a long time ago.>
38              
39             This module is intended to do for HTTP POST requests what
40             LWP::Simple did for GET requests. If you want to do anything
41             complicated, this module is not for you. If you just want to
42             push data at a URL with the minimum of fuss, you're the target
43             audience.
44              
45             =head1 METHODS
46              
47             =head2 post
48              
49             my $content = post( string $url, string $data );
50              
51             Posts the data in C<$data> to the URL in C<$url>, and returns
52             what we got back. Returns C on failure.
53              
54             =cut
55              
56             # I have added all sorts of hooks here in case you need to do
57             # anything complicated, BUT, if you do, you shouldn't be using
58             # this module...
59              
60             sub post {
61 0     0 1   my ( $url, $data, $params ) = @_;
62              
63             # Make the top secret params argument safe to use easily
64 0 0 0       $params = {} unless $params and ref $params;
65              
66             # Prepare the request itself
67 0           my $request = HTTP::Request->new( POST => $url );
68 0           $request->content($data);
69 0 0         $request->header( 'Content-type' => 'text/xml' ) if $params->{'as_xml'};
70 0 0         return $request if $params->{'return_request'};
71              
72             # Execute the request
73 0   0       my $ua = $params->{'ua'} || LWP::UserAgent->new();
74 0           my $response = $ua->request($request);
75 0 0         return $response if $params->{'return_response'};
76              
77             # Give the result to the user
78 0           my $content;
79 0 0         $content = $response->content if $response->is_success;
80 0           return $content;
81             }
82              
83             =head2 post_xml
84              
85             my $content = post_xml( string $url, string $data );
86              
87             Having written this module, it turned out that 99% of what I needed it
88             for required a content-type of C. This does exactly what C
89             does, only the content-type header is set to C.
90              
91             =cut
92              
93             sub post_xml {
94 0     0 1   my ( $url, $data ) = @_;
95 0           return post( $url, $data, { as_xml => 1 });
96             }
97              
98             =head1 AUTHOR
99              
100             Peter Sergeant - C
101              
102             =head1 COPYRIGHT
103              
104             Copyright 2011 Pete Sergeant.
105              
106             This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
107              
108             =cut
109              
110             1;