Clayton
Clayton

Reputation: 29

POSTing to form using LWP::UserAgent gets no response (mostly)

Here is my dilemma: I am trying to fill out a web form and get a result back from that form using LWP::UserAgent. Here is an example of my code:

#!/usr/bin/perl -w

use strict;
use LWP;
use HTTP::Request::Common;
use LWP::Debug qw(+);

my $ua = LWP::UserAgent->new(protocols_allowed=>["https"]);

my $req = POST 'https://their.securesite.com/index.php',
[ 'firstName'                   => 'Me',
  'lastName'                    => 'Testing',
  'addressLine1'                => '123 Main Street',
  'addressLine2'                => '',
  'city'                        => 'Anyplace',
  'state'                       => 'MN',
  'zipCode'                     => '55555',
  'card'                        => 'visa',
  'cardNumber'                  => '41111111111111111',
  'ccv2'                        => '123',
  'exp_month'                   => '07',
  'exp_year'                    => '2015',
  'shared_key'                  => 'hellos',
];

my $response = $ua->request($req);

print $response->is_success() . "\n";
print $response->status_line . "\n";
print $response->content . "\n";

When I run this, I get back a 200 OK and a "1" for success, but not the response page from the form. Just the closing tags:

</body>
</html>

Could this possibly be due to the fact that the form page and response page both have the same URL? I am new to LWP, so I am grasping at straws here. It may still be on the clients end, but I want to rule out any issues on my end as well.

Thanks in advance for any help you guys can give - I am Googled out.

Upvotes: 3

Views: 1370

Answers (3)

joehep
joehep

Reputation: 154

Use $response->decoded_content to get the content without the headers. See HTTP::Message for more information.

#!/usr/bin/perl -w

use strict;

use URI;
use LWP::UserAgent;
use HTTP::Request;

my $url = URI->new('https://their.securesite.com/index.php');
my $ua = LWP::UserAgent->new();

my $request = HTTP::Request->new(
    'POST',
    $url,
    HTTP::Headers->new(
        'User-Agent'  =>  "perl ua/ v0.001",
        'Accept'    =>  "text/xml, multipart/*, application/soap"
    ),
    [ 'firstName'                   => 'Me',
      'lastName'                    => 'Testing',
      'addressLine1'                => '123 Main Street',
      'addressLine2'                => '',
      'city'                        => 'Anyplace',
      'state'                       => 'MN',
      'zipCode'                     => '55555',
      'card'                        => 'visa',
      'cardNumber'                  => '41111111111111111',
      'ccv2'                        => '123',
      'exp_month'                   => '07',
      'exp_year'                    => '2015',
      'shared_key'                  => 'hellos',
     ]  
) or die "Error initiating Request: $@\n";

my $response = $ua->request( $request );

if ($response->is_success) {
     print $response->decoded_content, "\n";
} else {
    die $response->status_line;
}

Upvotes: 1

Joel Berger
Joel Berger

Reputation: 20280

If you can use Mojo::UserAgent (part of the Mojolicious suite of tools) the code would look like this. Note that you might need IO::Socket::SSL in order to use HTTPS.

#!/usr/bin/env perl

use strict;
use warnings;

use Mojo::UserAgent;

my $ua = Mojo::UserAgent->new;

my $tx = $ua->post('https://their.securesite.com/index.php', form => 
{ 'firstName'                   => 'Me',
  'lastName'                    => 'Testing',
  'addressLine1'                => '123 Main Street',
  'addressLine2'                => '',
  'city'                        => 'Anyplace',
  'state'                       => 'MN',
  'zipCode'                     => '55555',
  'card'                        => 'visa',
  'cardNumber'                  => '41111111111111111',
  'ccv2'                        => '123',
  'exp_month'                   => '07',
  'exp_year'                    => '2015',
  'shared_key'                  => 'hellos',
});

if ( $tx->success ) {
  print $tx->res->body;
  # or work with the resulting DOM
  # my $dom = $tx->res->dom;
} else {
  my ($err, $code) = $tx->error;
  print $code ? "$code response: $err\n" : "Connection error: $err\n";
}

The interface is a little different, but it has lots of nice features, including Mojo::DOM integration for parsing the response HTML.

Upvotes: 1

Dave Turner
Dave Turner

Reputation: 1

Check the value of $response->as_string

It'll show you full http response with headers

Upvotes: 0

Related Questions