Alister West

home is where your code is ...

Post Content to server

Sometime you just want to send stuff to a server. Solr runs off xml commands, and sometime you need to send feeds offsite. And sometimes I don't care about markup, syntax or libraries...

just post my handcrafted xml!


#!perl
use HTTP::Request;
use LWP::UserAgent;

sub post_to_server {

    my ($content, $url) = @_;

    my $req = HTTP::Request->new( "POST" => $url );
    $req->add_content( $content );

    # print $req->dump; 
    # print $req->as_string;

    my $ua = LWP::UserAgent->new();
    $ua->request($req);

} 
post_to_server( '<xml><foo>Foo</foo></xml>', 'http://example.com/uploads.cgi' );



# lwp-request - a handy linux tool
#
echo "<xml><foo>Foo</foo></xml>" | lwp-request -m PUT http://example.com/uploads.cgi




# NetCat - for debugging locally
#
# setup netcat to listen on localhost:12345
nc -l 12345

# In another terminal run a get/post/put/ as seen above
...

# NetCat output
#
alister@localhost ~ > nc -l 12345
PUT / HTTP/1.1
TE: deflate,gzip;q=0.3
Connection: TE, close
Host: localhost:12345
User-Agent: lwp-request/5.834 libwww-perl/5.834
Content-Length: 32
Content-Type: text/plain

<xml>
    <foo>Foo</foo>
</xml>
By Alister West