Perl Tutorials - Herong's Tutorial Examples - v6.01, by Dr. Herong Yang
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) HerongYang.com. All Rights Reserved. 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->max_redirect(5); $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(); print($req->as_string()); print($res->as_string()); } print($request->as_string()); print($response->as_string()); exit;
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:
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 x=1&y=2&z=3 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" action="https://www.facebook.com/login.php?login_attempt=1" 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:
herong> LWP-UserAgent-POST.pl https://www.facebook.com/login.php?login_attempt=1 "email=your_email&pass=your_pass" 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 email=your_email&pass=your_pass 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.
Table of Contents
Data Types: Values and Variables
Expressions, Operations and Simple Statements
Name Spaces and Perl Module Files
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 Directories and Read File Names
File System Functions and Operations
Socket Communication Over the Internet
XML::Simple Module - XML Parser and Generator
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
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
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
Converting Perl Script to Executable Binary