Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

URI::Escape::escape_char error when posting to a form inside perl script

by marknher (Initiate)
on Jan 31, 2013 at 05:31 UTC ( #1016213=perlquestion: print w/replies, xml ) Need Help??
marknher has asked for the wisdom of the Perl Monks concerning the following question:

I've used this code on my website for years without issue. Recently, it appears that my host upgraded the shared hosting server that I use and now I receive an error Undefined subroutine &URI::Escape::escape_char called at /usr/local/share/perl/5.10.1/URI/_query.pm line 51. This occurs when I post a form in perl. I realize that scalars I'm posting have characters that are reserved in URI even though this worked before, so I tried using uri_escape() to no avail. I've spent 10 hours or so now trying to figure this out. I am out of ideas and need your wisdom!

#!/usr/bin/perl use strict; use LWP::UserAgent; use HTTP::Request::Common; my $URLtoPostTo = "http://www.theurltopostto.com"; my %Fields = ( "data1" => "$email", "data2" => "$somethingelse", "data3" => "$andanother" ); my $BrowserName = "This Be Mine"; my $Browser = new LWP::UserAgent; if($BrowserName) { $Browser->agent($BrowserName); } my $Page = $Browser->request(POST $URLtoPostTo,\%Fields); #I don't need the results data
  • Comment on URI::Escape::escape_char error when posting to a form inside perl script
  • Download Code

Replies are listed 'Best First'.
Re: URI::Escape::escape_char error when posting to a form inside perl script
by kcott (Chancellor) on Jan 31, 2013 at 05:59 UTC

    G'day marknher,

    Welcome to the monastery.

    According to the HTTP::Request::Common documentation, the request() method, when used with POST, takes its list of key-value pairs as an arrayref:

    use HTTP::Request::Common; $ua = LWP::UserAgent->new; $ua->request(POST 'http://somewhere/foo', [foo => bar, bar => foo]);

    You're using a hashref (\%Fields). I'd suggest changing your request() call to:

    my $Page = $Browser->request(POST $URLtoPostTo, [%Fields]);

    -- Ken

      Thank you Ken, I've updated my code to include [] around %fields. Unfortunately, the error remains. From what I can tell, it's because the scalars in the pairs have reserved characters such as @, etc. Any other ideas? Thanks, Mark T.

        Take a look at the code for URI::Escape which you'll probably find at /usr/local/share/perl/5.10.1/URI/Escape.pm. In the current source for this module, escape_char() is coded right at the end:

        ... sub escape_char { return join '', @URI::Escape::escapes{$_[0] =~ /(\C)/g}; } 1;

        If it's not there, you'll need to ask your server administrators to update their code. If it is there, please show the actual code you are running, i.e. with the calls to uri_escape() - the problem may lie with your implementation.

        -- Ken

        If I comment out one of the key-pairs that have a reserved character such as $email (which has @ in it) then the script works fine. Thanks, Mark
Re: URI::Escape::escape_char error when posting to a form inside perl script
by Anonymous Monk on Jan 31, 2013 at 05:58 UTC
    install URI::Escape, upgrade LWP and all the prerequisites
      Unfortunately, since this is shared hosting server I'm unable to install or update anything on it. However, I'm not sure, but I'm assuming since I get an error regarding URI:Escape wouldn't that mean that some version is already installed? Thanks, Mark T.
Re: URI::Escape::escape_char error when posting to a form inside perl script
by Khen1950fx (Canon) on Jan 31, 2013 at 07:26 UTC
    I would try it like this:
    #!/usr/bin/perl use strict; use warnings; use LWP::UserAgent; use HTTP::Request::Common qw/POST/; my(@fields) = ( 'http://your.url.com', [ data1 => 'something', data2 => 'somethingelse', data3 => 'andanother' ] ); my $ua = LWP::UserAgent->new; my $req = POST @fields; print $ua->request($req)->as_string();
Re: URI::Escape::escape_char error when posting to a form inside perl script
by Anonymous Monk on Jan 31, 2013 at 23:32 UTC

    In my all night (again) of searching I finally found a workaround that solved my issue since the host admin was of no help. In case, this comes up in the future for anyone..

    Thank you monks for your help. I was literally lost for days and dreaming about this crap.

    use strict; use LWP::UserAgent; use HTTP::Request::Common; my $data1 = "$something"; my $data2 = "someone@email.com; my $data3 = "something"; my $prepstring = "data1=$data1\&data2=$data2\&data3=$data3"; my $ua = LWP::UserAgent->new(); my $response = $ua->post("$URLtoPostTo", Content => $preppedstring ); # This allows reserved characters such as @$ to pass through URI:Escap +e without issue I guess.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1016213]
Approved by vinoth.ree
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2018-11-19 05:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My code is most likely broken because:
















    Results (207 votes). Check out past polls.

    Notices?