powerhouse has asked for the wisdom of the Perl Monks concerning the following question:

I am trying desperately to send a form behind the scenes so I can get the response and send the visitor to where they need to go.
(Responds with a unique URL for the visitor to have to visit to order their commission card so we can put their commissions on their cards for them)

I've tried using both use Net::SSLeay qw(get_https post_https sslcat make_headers make_form); and use LWP::UserAgent, here is my code for both:

code for Net::SSLeay
use Net::SSLeay qw(get_https post_https sslcat make_headers make_form) +; my $page = ''; my $response = ''; my %headers = ''; use XML::Bare; my $server = 'api.path.com; my $path = '/further/http_path/fileName.asp?getInsteadOfPostVAR1='. +$username.'&getInsteadOfPostVAR2='.$password.'getInsteadOfPostVAR3='. +$accountId; my $port = '443'; # url encoded for redirect after card successfully ordered on the payo +neer website... my $_redirectURL = qq~https%3A%2F%2Fwww%2EencodedURL%2Enet%2Findex%2Ec +gi%3Fpg%3DBackOffice%26do%3Drfp%26un%3D$_username~; my $_xmlToSend = qq~<?xml version='1.0' encoding='UTF-8' ?><PayoneerDe +tails><Credentials><userName>$_apiUsername</userName><password>$_apiP +assword</password></Credentials><Details><prid>$_apiAccountId</prid>< +apuid>$_memberId</apuid><sessionid></sessionid><redirect>$_redirectUR +L</redirect><redirectTime>3</redirectTime><cardType>MC</cardType></De +tails><PersonalDetails><firstName>$_database->{FirstName}</firstName> +<lastName>$_database->{LastName}</lastName><dateOfBirth></dateOfBirth +><address1>$_database->{address}</address1><address2>$_database->{add +ress2}</address2><city>$_database->{city}</city><country>$_databaseco +untry</country><state>$_databasestate</state><zipCode>$_database->{zi +p}</zipCode><mobile>$_database->{cell}</mobile><phone>$_database->{ph +one}</phone><email>$_database->{email}</email></PersonalDetails></Pay +oneerDetails>~; my %submit_data = ( 'mname' => "GetXML", 'xml' => "$_xmlToSend", ); my $post_data = &make_form(%submit_data); my ($page,$response,%headers) = &post_https($server,$port,$path,'',$po +st_data); my $ob = new XML::Bare( text => "$page" ); my $root = $ob->parse(); my $_redirectUrl = $root->{PayoneerToken}{Token}{value}; #Get Data to see if it worked: die "\$_redirectUrl = '$_redirectUrl';\$post_data='$post_data';<br>\$p +age='$page';"; window_redirect($_redirectUrl); exit;
LWP Code:
#Using the same variables above for data: use LWP::UserAgent; my $ua = LWP::UserAgent->new; $ua->agent("Mozilla/8.0"); # pretend we are very capable browser $ua->cookie_jar({ file => "$ENV{HOME}/.cookies.txt" }); push @{$ua->requests_redirectable}, 'POST'; my $req = HTTP::Request->new(POST => 'https://'.$server.$path); $req->content_type('application/x-www-form-urlencoded'); $req->content('mname=GetXML&xml='.$_xmlToSend.'&submit=Choice'); $req->referer('https://www.OurDomainHere.net/index.cgi?pg=BackOffice') +; # Pass request to the user agent and get a response back my $res = $ua->request($req); if ($res->is_success) { $page=$res->content; } # Now go down to where xml::bare parses data.... and die to check if i +t works...
Both return the same response(displayed in browser from die statements):

$_redirectUrl = '';$post_data='p2=HiddenPassword&p3=HiddenAccountId&p1=HiddenUsername&mname=GetXML;
$page='<?xml version='1.0' encoding='ISO-8859-1' ?><PayoneerResponse><Error><Code>FFA009A</Code><Description>Auto populate XML contains invalid entries</Description></Error></PayoneerResponse>'

That is what prints from the die statement no matter which code I run...
Yet if I take the xml that I have post in a die statement elsewhere and put it in a form and post directly
through the form in the browser to that url, I get the correct xml response...

So something is wrong with this code, somewhere.

I think it may be the passing of a varible in a string: &xml=<?xml version='1.0' encoding='ISO-8859-1' ?> the spaces there may be the problem, not sure why... anyhow, do you know of a way to get that form posted as if a browser posted it directly without having to put the xml in a string which may cause errors on the receiving end?

I would greatly appreciate the advice of any of you monks or visitors that have a clue...

Thanks,
Richard

Replies are listed 'Best First'.
Re: Posting xml in a form behind the scenes
by Corion (Patriarch) on Dec 15, 2009 at 08:35 UTC

    You don't show us the likely core of the problem, the post_https routine. Most likely it doesn't properly encode your POST data. I think you should look at the difference of what your web browser sends (for example using the Live HTTP Headers extension for Firefox, or wireshark for everything), and what your Perl script sends. Then it will all become clear to you.