Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


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

Posted on 2016-07-27
Medium Priority
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.

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

Question by:ou81aswell
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 28

Expert Comment

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 .

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


Author Comment

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?

LVL 28

Expert Comment

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

Author Comment

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

Accepted Solution

FishMonger earned 2000 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.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Six Sigma Control Plans

604 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