Solved

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

Posted on 2016-07-27
5
62 Views
Last Modified: 2016-08-18
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

0
Comment
Question by:ou81aswell
  • 3
  • 2
5 Comments
 
LVL 28

Expert Comment

by:FishMonger
ID: 41733198
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

0
 

Author Comment

by:ou81aswell
ID: 41733279
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
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 41733315
Have you read the $ua->cookie_jar section in the LWP::UserAgent module documentation?
0
 

Author Comment

by:ou81aswell
ID: 41733346
Bits of it. I was hoping this was a trivial thing that could be accomplished in 2 or 3 lines of code.
0
 
LVL 28

Accepted Solution

by:
FishMonger earned 500 total points
ID: 41733366
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.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now