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.

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

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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 .

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

ou81aswellAuthor Commented:
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?

Have you read the $ua->cookie_jar section in the LWP::UserAgent module documentation?
ou81aswellAuthor Commented:
Bits of it. I was hoping this was a trivial thing that could be accomplished in 2 or 3 lines of code.
It probably will end up being a few simple lines of code, but I haven't done any cookie coding through LWP::UserAgent so I would have to do some testing.

The starting point would be to read the documentation in LWP::UserAgent and HTTP::Cookies to learn the required syntax for this task.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.