Avatar of John
JohnFlag for Canada

asked on 

Perl - Passing cookies received with CGI::Cookie through to and from LWP::UserAgent->Post

I'm running Perl CGI in an Apache environment.

My Perl script calls another page on my domain using LWP::UserAgent->Post. It takes the html that is returned and passes it back (prints it) to my user.

I would like to pass all of the cookies sent to my script on to the page I'm posting to. I'd also like to send back any cookies returned from the page I posted to, back to my user.

The purpose of my script is to add some credentials to the http post data so we can avoid storing them as hidden fields on our web pages.

#!/usr/bin/perl
use CGI;
$q = CGI->new;

use LWP::UserAgent;
$ua = LWP::UserAgent->new;

# how do I copy the cookies passed to this script to the UserAgent so I can pass them to $url?
my $result = $ua->post($url, \%PostData ); 

if ($result->is_success) {
	# how do I copy the cookies returned from $url back to my user?
	print "Content-Type: text/html\n\n";
	print $result->decoded_content;
}

Open in new window

Perl

Avatar of undefined
Last Comment
FishMonger
Avatar of FishMonger
FishMonger
Flag of United States of America image

IMO, you're attempting to use a very convoluted approach to avoid using hidden fields.

I too don't like using hidden fields so, Instead you should be using server side sessions .
CGI::Session
or
Data::Session

On an unrelated note, you should ALWAYS include the warnings and strict pragmas in EVERY perl script and while testing/debugging cgi scripts, you should include the CGI::Carp module..
use strict;
use warnings;
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);

Open in new window

Avatar of John
John
Flag of Canada image

ASKER

Thanks. This behavior is the recommended approach / best practice for our new payment gateway (Elavon Converge). I'm adding support for this gateway to our shopping cart as our old gateway (bought out by Elavon) is being retired at the end of next month.

From what I can tell, it's pretty new (the recommendation for what they call server side scripting, to be a best practice) as the perl samples they provide gloss over the issues introduced when you call their pages indirectly, like cookies.

I saw something like the following recently:

%cookies = CGI::Cookies->Fetch;

This looks like a way of grabbing all of the cookies passed.

Is there a way to hook that up to the UserAgent->Post call?

Thanks
Avatar of FishMonger
FishMonger
Flag of United States of America image

Have you read the $ua->cookie_jar section in the LWP::UserAgent module documentation?
Avatar of John
John
Flag of Canada image

ASKER

Bits of it. I was hoping this was a trivial thing that could be accomplished in 2 or 3 lines of code.
ASKER CERTIFIED SOLUTION
Avatar of FishMonger
FishMonger
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Perl
Perl

Perl is a high-level, general-purpose, interpreted, dynamic programming languages with over 25 years of development. Perl 5 runs on over 100 platforms from portables to mainframes and is suitable for both rapid prototyping and large-scale development projects. Perl gained widespread popularity as a Common Gateway Interface (CGI) scripting language, in part due to its regular expression and string parsing abilities. In addition to CGI, Perl is used for graphics programming, system administration, network programming, finance, bioinformatics, and other applications.

26K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo