How can I send an HTTP POST request using only standard Perl libraries?

Suppose a system containing only a basic Perl installation without any additional CPAN modules (LWP, therefore NOT ). Some of my target environments have limited space, and I cannot increase my footprint at all (otherwise I would use LWP).

What is the most portable and reliable way to issue an HTTP POST request from such a system and then get a response?

+4
source share
4 answers

HTTP :: Lite is pure-Perl, so you can simply link it with existing code.

+16
source

Consider this. HTTP POSTs are not trivial. You need to collect the data in the MIME block, encode it correctly, then open the Socket connection, send the data, and then read any response headers from the socket to make sure they work.

You will do a lot of work to duplicate what LWP is already doing, and then you take all this work and put it in your environment that does not have LWP.

At this point, you ask yourself: "Lord, if I can put my own Perl code in this environment, why can't I just put LWP there?"

Do not be afraid, because I am here to save you three months of worthless work.

How to install Perl modules locally

If you can’t do this,

How to use PAR to package and distribute dependencies

Good luck, and do not duplicate the code.

+12
source

See if there are other programs available that you could call to execute this entry. curl, wget, lynx or Java, for example.

+3
source

I am just doing an HTTP message like this without using any library,

sub post { local($host,$port,$request,$data) = @_; ($fqdn, $aliases, $type, $len, $thataddr) = gethostbyname($host); $that = pack($sockaddr, &AF_INET, $port, $thataddr); socket(FS, &AF_INET, &SOCK_STREAM, $proto) || return undef; bind(FS, $thissock) || return undef; local($/); unless (eval q! $SIG{'ALRM'} = "timeout"; alarm($timeout); connect(FS, $that) || return undef; select(FS); $| = 1; select(STDOUT); print FS "POST $request HTTP/1.0\r\n$referer"; print FS "$useragent"; print FS "Host: $host:$port\r\n$mimeaccept"; print FS "$cnt_type"; $len = length($data); print FS "Content-length: $len\r\n\r\n$data\r\n"; undef($page); $/ = "\n"; $_ = <FS>; if (m:HTTP/1.0\s+\d+\s+:) { #HTTP/1.0 while(<FS>) { last if /^[\r\n]+$/; # end of header } undef($/); $page = <FS>; } else { # old style server reply undef($/); $page = $_; $_ = <FS>; $page .= $_; } $SIG{'ALRM'} = "IGNORE"; !) { return undef; } close(FS); $page; } 

I use this with a real old server (Apache httpd first generation). It does not support HTTP / 1.1, encoded encoding, etc., but you get this idea.

+2
source

All Articles