Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Shortest/quickest way for Perl to take POST data it receives and send a POST request with this data to another URL?

by tunafish (Beadle)
on Nov 05, 2013 at 06:29 UTC ( #1061251=perlquestion: print w/replies, xml ) Need Help??
tunafish has asked for the wisdom of the Perl Monks concerning the following question:

I need a script to receive a POST request and then to send that same POST request to a different script. I don't mean that this is a redirect: the user interacts only with script 1; script 2 does not return any data.

What's the quickest and dirtiest way of doing this? A possible solution is, of course, to parse the data using CGI, then to form and send a POST request using LWP, but this seem un-Perl-like and inelegant. Surely there's a simpler solution?

Edited to add: this needs to be accomplished via a POST request to script 2. Not, say, via exec.

Edited to add 2: I think I need to re-phrase the question. How about this:

Is there a way to take the request that was used to access a cgi script and turn it into an HTTP::Request object?

  • Comment on Shortest/quickest way for Perl to take POST data it receives and send a POST request with this data to another URL?

Replies are listed 'Best First'.
Re: Shortest/quickest way for Perl to take POST data it receives and send a POST request with this data to another URL?
by derby (Abbot) on Nov 05, 2013 at 11:57 UTC

    CGI and LWP is very perlish especially since the scripts are on different machines -- that makes HTTP the only path (unless you make machine A a reverse proxy for machine B).

    -derby
Re: Shortest/quickest way for Perl to take POST data it receives and send a POST request with this data to another URL?
by zentara (Archbishop) on Nov 05, 2013 at 16:21 UTC
    I need a script to receive a POST request and then to send that same POST request to a different script

    I may be completely misunderstanding what you want, but here is something you might look at. I was using it to test eccomerce transactions. Change the url's to fit your needs.

    #!/usr/bin/perl use warnings; use CGI 'cgi'; use LWP::UserAgent; # from a config file our ($secure_server_address,$cgi_directory); print "Content-type: text/html\n\n"; #my $test= 'FALSE'; my $test= 'TRUE'; my $relay; my $cgi = new CGI; my %input= $cgi->Vars(); foreach $name (keys %input){ $value = $input{$name}; $relay .= "$name=$value&"; } $relay .= "Ecom_transaction_complete=$test&"; $relay .= "IOC_response_code=0&"; open (RT,">test/respgen.test"); print RT $relay; close RT; my $ua = LWP::UserAgent->new(); my $req = HTTP::Request->new (POST => "$secure_server_address$cgi_dire +ctory/boacc.pl"); $req->content_type('application/x-www-form-urlencoded'); $req->content("$relay"); my $res = $ua->request($req); print $res->as_string;

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: Shortest/quickest way for Perl to take POST data it receives and send a POST request with this data to another URL?
by BrowserUk (Pope) on Nov 05, 2013 at 06:57 UTC
    I need a script to receive a POST request and then to send that same POST request to a different script. I don't mean that this is a redirect; the script simply notifies another script.

    In theory, the first script could just immediately exec the second script. The second script would inherit the same environment (ie. CONTENT_LENGTH) and STDIN (from which it can read the POST data).

    This would be very simple & efficient. The first script would (in theory) need be nothing more than:

    #! perl exec 'theSecond.pl';

    But then, what would be the point? Why not just put the code from the second script in the first avoid the need for the exec?


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Scripts are on different machines, so this solution is not viable.

        Scripts are on different machines,

        You should make that clear in your question then.

        Is it under your control? Do you have NFS access to it?

        If so, you could still exec it.

        However, if that other machine is another web-server and you need to invoke the remote script via its url, then it makes a lie of "I don't mean that this is a redirect;", because that is exactly what a redirect does.

        Clear questions get good answers.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Shortest/quickest way for Perl to take POST data it receives and send a POST request with this data to another URL?
by Anonymous Monk on Nov 05, 2013 at 08:39 UTC

    What's the quickest and dirtiest way of doing this? A possible solution is, of course, to parse the data using CGI, then to form and send a POST request using LWP, but this seem un-Perl-like and inelegant. Surely there's a simpler solution?

    Proxy? proxy

Re: Shortest/quickest way for Perl to take POST data it receives and send a POST request with this data to another URL?
by Your Mother (Chancellor) on Nov 05, 2013 at 19:06 UTC

    As someone mentioned, the easiest/shortest way is with a proxy; e.g., Plack::App::Proxy

    Plack::App::Proxy->new(remote => 'http://perl.org')->to_app;

    That could be wrapped up in a proper script to control URIs and capturing data and what gets proxied and what doesn't, etc.

    Because I was curious, I tried whipping up a Plack::Request/HTTP::Response based version. Turns out it's quite easy. Though it seems to work, this code is a simplistic first stab and I have no idea how fit it is for your, or any, actual task.

    The code is shown after the usage… I admit it's confusing, but it was more fun/terse to have the code display itself.

    Run it

    I stripped some headers (Host/Agent/Accept) from the output to save space.

    plackup /home/moo/post-echo.psgi -r Watching /home/moo/lib /home/moo/post-echo.psgi for file updates. HTTP::Server::PSGI: Accepting connections at http://0:5000/

    Use it

    moo@cow[2258]~>curl http://0:5000/ OHAI GET moo@cow[2259]~>curl http://0:5000/ -X HEAD OHAI HEAD moo@cow[2260]~>curl http://0:5000/ -X POST POST http://example.org/ User-Agent: ...snip... moo@cow[2261]~>curl http://0:5000/ -d "internet=cats in tubes" POST http://example.org/ Content-Length: 22 Content-Type: application/x-www-form-urlencoded internet=cats in tubes

    Have it echo its own code :P

    Demonstrate that file uploads (apparently) work fine too.

    curl http://0:5000/ -F code=@/home/moo/post-echo.psgi POST http://example.org/ Expect: 100-continue Content-Length: 2408 Content-Type: multipart/form-data; boundary=-------------------------- +--2db0025e93ba ------------------------------2db0025e93ba Content-Disposition: form-data; name="code"; filename="post-echo.psgi" Content-Type: application/octet-stream #!/usr/bin/env perl # filename: post-echo.psgi use strictures; use Plack::Request; use HTTP::Request; sub { my $env = shift; my $request = Plack::Request->new($env); my $response = $request->new_response(200); $response->content_type("text/plain; charset=UTF-8"); if ( $request->method eq "POST" ) { $request->input->read( my $buffer, $request->header("Content-L +ength"), 0 ); my $rerequest = HTTP::Request ->new( "POST", "http://example.org/" , $request->headers, +$buffer ); $response->body( $rerequest->as_string ); } else { $response->body("OHAI " . $request->method . "\n"); } $response->finalize; };
Re: Shortest/quickest way for Perl to take POST data it receives and send a POST request with this data to another URL?
by marinersk (Priest) on Nov 05, 2013 at 17:59 UTC
    Hello tunafish,

    I need a script to receive a POST request and then to send that same POST request to a different script. I don't mean that this is a redirect: the user interacts only with script 1; script 2 does not return any data.

    I'm already confused. (For example, how does one send a POST request to a script? I can guess what you mean by that, but it messes with my head a bit.)

    My best guess: You're trying to store a copy of all requests that come through the system.

    If I've guessed correctly, I'd suggest you look to:

    • LWP::UserAgent
    • LWP::Simple

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1061251]
Approved by Corion
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2017-12-16 19:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (458 votes). Check out past polls.

    Notices?