Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Saving parameters before sending to 3rd party

by jonnyfolk (Vicar)
on Mar 02, 2004 at 16:07 UTC ( [id://333290]=perlquestion: print w/replies, xml ) Need Help??

jonnyfolk has asked for the wisdom of the Perl Monks concerning the following question:

I have a simple HTML form which collects some information from the user and transmits it to a third party. I would like to keep a copy of the information sent.

I don't want the user to have to click another submit button to send it on its way, so I can't reassemble it into another form, so I'm looking for a device which would let me forward the information to the third party script on receipt of the form.

How can I do this?

Update: Should've mentioned that I need a POST method - apparently LWP::UserAgent can do it...

Replies are listed 'Best First'.
Re: Saving parameters before sending to 3rd party
by matija (Priest) on Mar 02, 2004 at 16:17 UTC
    Depends on what the request looks like. If you make a GET request, (one where the parameters are transmitted within the URL), you could have it first go to your script, you record it, and then issue a redirect
    use CGI; (store parameters etc); print $cgi->redirect('the.other.host/url-with-parameters');
    I don't think that method will work if you have a POST method in your form.

    In that case, you could accept the request in your script, record the parameters, then do a LWP::Simple request in the background, and send the results of that request to the client. Note that in that case, the other server will see ALL the requests as coming from your server's IP - which may not be what you want.

    Update:Right, as b10m pointed out, LWP::Simple doesn't do POST, only get (the fact that it accepts the URL as the single parameter should have clued me in). Yes, you'd have to use LWP::UserAgent, and it's post method.

      Thanks very much matija - I shall check out LWP::UserAgent.
Re: Saving parameters before sending to 3rd party
by TomDLux (Vicar) on Mar 02, 2004 at 18:10 UTC

    Maybe my brain has been damaged by lack of caffiene, but it seems to me the other monks are answering a different question than the one you asked.

    Current Situation

    You have a web page which displays a form. When the user completes the form, it is sent to a CGI script, which transmits the information to a third party.

    Of course, other possibilities exist; for example, that your web site displays a page containing a form, but when the Submit button is pressed, the data goes tot he third party, without ever coming to your site.

    Desired Change

    You wish the data to be saved at your system.

    Trivial Solution

    Is it possible to configure Apache so that the POST arguments are made part of the log? Is it possible the information already is being logged?

    Less Trivial Solution

    When the button is pressed, a CGI script is invoked. Modify the script so it saves the data before sending it off to the third party.

    Somewhat Tougher Solution

    If the data is going directly to the third party, modify the web page containing the form, so it invokes your CGI script instead of someone else's. Your script can save the data, then re-direct the user to the remote script, or it can invoke the remote script "secretly" and then pass the return values to the user.

    --
    TTTATCGGTCGTTATATAGATGTTTGCA

Re: Saving parameters before sending to 3rd party
by jonnyfolk (Vicar) on Mar 02, 2004 at 16:51 UTC
    use HTTP::Request::Common; $ua = LWP::UserAgent->new; $ua->request(POST 'http://somewhere/foo', [foo => bar, bar => foo]);
    would seem to be very promising for this - thanks for the help, all.
Re: Saving parameters before sending to 3rd party
by blue_cowdawg (Monsignor) on Mar 02, 2004 at 16:12 UTC

        collects some information from the user and transmits it to a third party.

    Sends it to a third party, how? Have you tried the redirect method of CGI?


    Peter L. Berghold -- Unix Professional
    Peter at Berghold dot Net
       Dog trainer, dog agility exhibitor, brewer of fine Belgian style ales. Happiness is a warm, tired, contented dog curled up at your side and a good Belgian ale in your chalice.
Re: Saving parameters before sending to 3rd party
by Anonymous Monk on Mar 02, 2004 at 22:55 UTC

    Here's what you want, assuming that the 3rd party is allowing the data to be sent as a GET request:

    #!c:/perl/bin/perl -w $|++; use strict; use CGI::Simple; use URI::Escape; my $CGI = CGI::Simple->new(); # save your copy of the information here # using whatever method you prefer # now we redirect the user to the 3rd party with the data my $query = join( ';', map { uri_escape($_) . '=' . uri_escape($CGI->param($_)) } $CGI->param() ); print $CGI->redirect( 'http://their.server.com/foo/bar.pl?' . $query );

      In spirit of permitting multiple values to the same parameter name, change the query builder I provided to this new one:

      my $query = join(';', map { my $k = shift @$_; join(';', map { uri_escape($k) . '=' . uri_escape($_) } @$_) } map { [$_, $CGI->param($_)] } $CGI->param() );
        You could also do:

        my $query = join(';', map { my $k = $_; join(';', map { uri_escape($k) . '=' . uri_escape($_) } $CGI->param($_)) } $CGI->param() );
Re: Saving parameters before sending to 3rd party
by Hagbone (Monk) on Mar 02, 2004 at 23:00 UTC
    You don't mention how things are being sent to the third party ....

    I'm thinking along the same lines as TomDLux .... unless I'm missing something, couldn't you write the info you send to the third party to a (log) file?

    Typically (in my situations), someone "submits" from a form, and the output is emailed to the third party, and the event (and any info needed) is written to a file...

    in the script, there's something like:

    open(MAIL,"|$mailprog -t"); . . print MAIL $q->param('WHATEVER'),"\n";
    (you get the idea) .. and then needed info is written to a file ...
    open (LOGFILE,">>$logfile") || die "NFG $logfile!\n"; . . . print LOGFILE $q->param('WHATEVER')."\n";
    In fact, I think I have a script somewhere that prints the same content to file that is sent via email to the third party, using a loop.

    Hell, if your emailing the output to the third party, you could BCC yourself ... but again, you don't mention how you're sending the info to the thrid party, so it's tough to tell if the solution could be *that* siumple.

    Maybe I'm missing something ?? , but this appears to be question simple enough that even *I* have offered a response ;)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://333290]
Approved by blue_cowdawg
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-04-19 13:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found