LWP-UserAgent-POST.pl - Posting Form Data

This section provides a tutorial example on how to send a POST request to a Web server with form input data, and dump all request and response messages.

As mentioned in the previous tutorial, the next step to play with facebook server is to send a POST with user login information.

Here is my first version of LWP-UserAgent-POST.pl:

#- LWP-UserAgent-POST.pl
#- Copyright (c) 2014 by Dr. Herong Yang, http://www.herongyang.com/
   use LWP::UserAgent;

   my ($uri, $data) = @ARGV;
   $uri = 'http://herongyang.com/' unless $uri;
   $data = 'author=Herong&lang=Perl' unless $data;

   my $browser = new LWP::UserAgent();
   $browser->agent("Mozilla/5.0 (Windows NT 6.1)");
   @data = split(/[=&]/,$data);
   my $response = $browser->post($uri, \@data);
   my $request = $response->request();
   my @redirects = $response->redirects();
   foreach my $res (@redirects) {
      my $req = $res->request();


Notice that how the command line "$data" argument is converted into an array "@data" and passed to the post() method as a reference.

Test 1: When testing the program on google.com, I got the following:

C:\herong>LWP-UserAgent-POST.pl http://www.google.com "x=1&y=2&z=3"

POST http://www.google.com
User-Agent: Mozilla/5.0 (Windows NT 6.1)
Content-Length: 18
Content-Type: application/x-www-form-urlencoded


HTTP/1.1 405 Method Not Allowed
Connection: close
Allow: GET, HEAD
Content-Length: 1453
Content-Type: text/html; charset=UTF-8
Alternate-Protocol: 80:quic
Client-Response-Num: 1
Title: Error 405 (Method Not Allowed)!!1
X-Frame-Options: SAMEORIGIN
X-Meta-Charset: utf-8
X-Meta-Viewport: initial-scale=1, minimum-scale=1, width=device-width
X-XSS-Protection: 1; mode=block

<!DOCTYPE html>
<html lang=en>

The response tells us that:

Test 2: Now try LWP-UserAgent-POST.pl on facebook.com. We can pick up the login POST URI and input variable names from the response in the previous tutorial:

<form id="login_form" 
method="post" onsubmit="return window.Event && Event.__inline...
<input type="text" class="inputtext" name="email" id="email" value...
<input type="password" class="inputtext" name="pass" id="pass" tab......

Here is the test result:


POST https://www.facebook.com/login.php?login_attempt=1
User-Agent: Mozilla/5.0 (Windows NT 6.1)
Content-Length: 46
Content-Type: application/x-www-form-urlencoded

HTTP/1.1 200 OK
Cache-Control: private, no-cache, no-store, must-revalidate
Connection: close
Client-Response-Num: 1
Client-SSL-Cert-Issuer: /C=US/O=VeriSign, Inc./OU=VeriSign Trust N...
Title: Facebook

<!DOCTYPE html>
<html lang="en" id="facebook" class="no_js">
<div class="fsl fwb fcb">Cookies Required</div>
<div>Cookies are not enabled on your browser. Please enable coo...

Cool, facebook.com accepted our POST call! However, it returned a new error "Cookies Required" in the HTML body. This tells us that facebook.com requires us to attach cookies it gave to us in the previous GET response. So we need to enhance LWP-UserAgent-POST.pl to attach cookies. See the next tutorial.

Last update: 2014.

Table of Contents

 About This Book

 Perl on Linux Systems

 ActivePerl on Windows Systems

 Data Types: Values and Variables

 Expressions, Operations and Simple Statements

 User Defined Subroutines

 Perl Built-in Debugger

 Name Spaces and Perl Module Files

 Symbolic (or Soft) References

 Hard References - Addresses of Memory Objects

 Objects (or References) and Classes (or Packages)

 Typeglob and Importing Identifiers from Other Packages

 String Built-in Functions and Performance

 File Handles and Data Input/Output

 Open Files in Binary Mode

 Open Directories and Read File Names

 File System Functions and Operations

 Converting Perl Script to Executable Binary

 Using DBM Database Files

 Using MySQL Database Server

 Socket Communication Over the Internet

 XML::Simple Module - XML Parser and Generator

 XML Communication Model

 SOAP::Lite - SOAP Server-Client Communication Module

 Perl Programs as IIS Server CGI Scripts

 CGI (Common Gateway Interface)

 XML-RPC - Remote Procedure Call with XML and HTTP

 RPC::XML - Perl Implementation of XML-RPC

 Integrating Perl with Apache Web Server

 CGI.pm Module for Building Web Pages

LWP::UserAgent and Web Site Testing

 What Is LWP::UserAgent?

 What Is HTTP::Response?

 What Is HTTP::Request?

 LWP-UserAgent-GET.pl - Sending a GET Request

 LWP-UserAgent-GET-Redirect.pl - Following HTTP Redirects

 http-equiv="Refresh" Meta Tag not Followed

LWP-UserAgent-POST.pl - Posting Form Data

 post() Method not Following Redirect Location

 LWP-UserAgent-POST-Redirect.pl - Posting with Redirects

 What Is HTTP::Cookies?

 LWP-UserAgent-Request.pl - GET, POST and Cookies

 LWP-UserAgent-Request.pl - Login to facebook.com

 HTTP::Cookies save() not Saving Temporary Cookies

 LWP-UserAgent-Request-Cookies.pl - Sending Request with Cookies


 PDF Printing Version