in reply to Re: Passing POST parameters
in thread Passing POST parameters

All right then... so I suppose I will write you up a short example of POST-ing to a web server, w/o using modules (except for Socket, but you *really* should have that one)--(and not that you really asked :).

WARNING: this may not be great code. But I think it's got the essentials of what you need to do to write a client that POSTs to a web server and receives a response.

use Socket; # remote hostname and port (80, most likely) my $remote = "www.server.com"; my $port = 80; # the URL you want to POST to my $url = "/cgi-bin/form.pl"; # the content you want to POST # (key value pairs, most likely) my $content = "id=103"; # network stuff--lookup host name and convert # it into a packed IP address suitable for # passing to connect. then get the protocol # data needed by socket (for the "tcp" protocol). my $paddr = sockaddr_in $port, inet_aton $remote; my $proto = getprotobyname('tcp'); # open up a socket on your local machine socket S, &AF_INET, &SOCK_STREAM, $proto or die "Can't open socket: $!"; # connect your local socket to the remote host and port connect S, $paddr or die "Can't connect: $!"; # make your socket unbuffered select((select(S), $|=1)[0]); # print POST line to web server, followed by # Content-Length header--this header is essential # so that the web server knows that content will # be sent along in the body of the request print S "POST $url HTTP/1.0\n"; print S "Content-Length: ", length $content, "\n"; print S "\n"; print S $content, "\n\n"; # you've sent the request, now just read back the # response, headers and all, and print it print while <S>; # close up the socket close S;

Replies are listed 'Best First'.
Re: RE: Re: Passing POST parameters
by Anonymous Monk on Jul 24, 2001 at 17:46 UTC
    This, or the LWP program, works great! However, how would you do that to a program on a Secure Socket layer (https://foo.com/bar.pl)?
Re: RE: Re: Passing POST parameters
by Indira (Initiate) on Apr 30, 2001 at 15:42 UTC
    Wow! Thanks for this script. It happens to be exactly what I was looking for because I needed to write a cron job that passes certain parameters to an ASP script. This of course had to be done without user interaction of pressing a 'submit' button.