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

I'm probably missing something really simple here, but is there any way or module i can use to easily send POST data to a remote CGI script? I don't care about capturing the output so much, i just want to send the request. I'm writing a little hack for movabletype so every time i make an entry it auto-posts it on my old xanga (for people too lazy to go to my new blog). Thanks, -fibbi
  • Comment on post data to a remote cgi script with a perl script?

Replies are listed 'Best First'.
Re: post data to a remote cgi script with a perl script?
by SciDude (Friar) on Jun 02, 2004 at 01:05 UTC

    I suggest a study of the LWP - Library for WWW access in perl.

    Here is a short quote from the docs:

    The libwww-perl collection is a set of Perl modules which provides a simple and consistent application programming interface (API) to the World-Wide Web. The main focus of the library is to provide classes and functions that allow you to write WWW clients. The library also contain modules that are of more general use and even classes that help you implement simple HTTP servers.

    Most modules in this library provide an object oriented API. The user agent, requests sent and responses received from the WWW server are all represented by objects. This makes a simple and powerful interface to these services. The interface is easy to extend and customize for your own needs.

    And also from the docs:

    # Create a user agent object use LWP::UserAgent; $ua = LWP::UserAgent->new; $ua->agent("MyApp/0.1 "); # Create a request my $req = HTTP::Request->new(POST => 'http://search.cpan.org/search' +); $req->content_type('application/x-www-form-urlencoded'); $req->content('query=libwww-perl&mode=dist'); # Pass request to the user agent and get a response back my $res = $ua->request($req); # Check the outcome of the response if ($res->is_success) { print $res->content; } else { print $res->status_line, "\n"; }

    Good luck and post your code when you get it working!

    SciDude
Re: post data to a remote cgi script with a perl script?
by meetraz (Hermit) on Jun 02, 2004 at 00:58 UTC
    Here you go:

    use strict; use HTTP::Request::Common; use LWP::UserAgent; my $ua = LWP::UserAgent->new; $ua->request(POST 'http://somewhere/foo.cgi', [foo => "bar", bar => "foo"]);