in reply to Passing POST parameters

That *is* actual code. Do you mean code that doesn't use modules? Modules use actual code, too. Do you not have LWP? It comes with Perl.

I'm not going to write out an entire POST application w/o using modules... sorry. The basic idea is that you want to open up a socket (using socket) to the remote host, send the request headers, then send the content of the request. Then you read back the response. perlipc has some examples of using sockets to read from and write to tcp/ip servers. You'll also want to read the HTTP specification.

Personally, I think you'd be better off figuring out why LWP doesn't work for you than completely rewriting it. It's not that I'm against learning for learning's sake-- I agree with japhy's comment that you should know how a module works, and understand it, before you actually use it--but I don't think that you should actually *rewrite* a standard module.

If you can post specific problems about why "modules never work for you", I'd be happy to try and offer some suggestions.

Replies are listed 'Best First'.
RE: Re: Passing POST parameters
by chromatic (Archbishop) on Apr 04, 2000 at 07:27 UTC
    I don't think LWP is in the 5.005 core... I certainly had to download it on at least two of my Linux boxen. Maybe I should have compiled my own?
      Ah. Perhaps not. Perhaps I did have to install it specially. Although not for MacPerl, but then, that's special. :)

      All the same, though, it's relatively standard.

      Ah. Perhaps not. Perhaps I did have to install it specially. Although not for MacPerl, but then, that's special. :)

      All the same, though, it's relatively standard.

RE: Re: Passing POST parameters
by btrott (Parson) on Apr 04, 2000 at 09:37 UTC
    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;
      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)?
      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.