Solved

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

Posted on 2016-07-27
5
91 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

813 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

10 Experts available now in Live!

Get 1:1 Help Now