views:

114

answers:

2

Hello! The following script works fine:

#!/usr/bin/env perl
use strict; use warnings;
use Data::Dumper;
use WWW::Mechanize;

my $loginData = "userName=username&password=password&deeplinkForward=%2Fselfcare%2Frestricted%2FprepareCoCo.do&x=84&y=7";
my $loginUrl = "https://www.login.login/login.do";
my $mech = WWW::Mechanize->new( show_progress => 1 );

my $req = $mech->post( $loginUrl, 'Content' => $loginData  );

my $content = $req->content();
print Dumper $content;

But when I replace the line

my $req = $mech->post( $loginUrl, 'Content' => $loginData  );

with

my %hash = (    
    'username' => 'username', 
    'password' => 'password', 
    'deeplinkForward' => '%2Fselfcare%2Frestricted%2FprepareCoCo.do', 
    'x' => '84', 
    'y' => '7' 
);

my $req = $mech->post( $loginUrl, 'Content' => \%hash );

it doesn't work any more ( the script works, but the login doesn't ). Is there something worng?

A: 

i don't have mechanize in place, but you can try this and see how it goes

my $req = $mech->post( $loginUrl, \%hash);
ghostdog74
I tried this already and it doesn't work either.
sid_com
+5  A: 

You have to unescape deeplinkForward:

'deeplinkForward' => '/selfcare/restricted/prepareCoCo.do',

Otherwise, WWW::Mechanize thinks you want to send literal % signs, and helpfully escapes them for you.

To see what's going wrong, try adding this code right before the $mech->post line:

use HTTP::Request::Common 'POST';
print POST( $loginUrl, 'Content' => $loginData )->as_string;
print POST( $loginUrl, 'Content' => \%hash )->as_string;

They should be the same, except for the order of the fields.

It's conceivable that the server requires the fields to be listed in that order (it shouldn't, but...). In that case, you can use an array instead of a hash (hashes don't preserve ordering). Just replace %hash with @fields everywhere it appears.

print POST( $loginUrl, 'Content' => \@fields )->as_string;
cjm
It still doesn't work.
sid_com
Do username or password contain any % characters? You'd have to unescape them too.
cjm
Thx, it was the key “username” which was wrong – it should be “userName”.
sid_com
Yep, I missed the userName bug. I compared the two outputs in my editor, but I had case-insensitive search turned on. At any rate, `$mech->post` just passes its parameters to `HTTP::Request::Common::POST`, which makes that useful to see how it's building your requests.
cjm
The userName bug was the second error; as you explained, the first error was using "%2F" instead of "/".
sid_com