Perl Tutorials - Herong's Tutorial Examples - v6.01, by Dr. Herong Yang
LWP-UserAgent-POST-Redirect.pl - Posting with Redirects
This section provides a tutorial example on how to send a POST request to a Web server with form input data, follow HTTP redirects, and dump all request and response messages .
To understand why post() method does not follow HTTP redirects, I checked the LWP::UserAgetn document again and found that the default setting of "requests_redirectable" is ['GET', 'HEAD'].
So to force the post() method to follow HTTP redirects, we just need to change "requests_redirectable" setting to ['GET', 'HEAD', 'POST'] as shown in this enhanced script, LWP-UserAgent-POST-Redirect.pl:
#- LWP-UserAgent-POST-Redirect.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->requests_redirectable(['GET', 'HEAD', 'POST']); $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;
Now repeat the same test from the previous tutorial with this new script:
herong> LWP-UserAgent-Post-Redirect.pl http://localhost/CGI-pm-Redirect-URI.pl uri=http://localhost/hello.html POST http://localhost/CGI-pm-Redirect-URI.pl User-Agent: Mozilla/5.0 (Windows NT 6.1) Content-Length: 39 Content-Type: application/x-www-form-urlencoded uri=http%3A%2F%2Flocalhost%2Fhello.html HTTP/1.1 302 Found Connection: close Location: http://localhost/hello.html Server: Apache/2.2.25 (Win32) Content-Length: 0 Content-Type: text/plain Client-Response-Num: 1 GET http://localhost/hello.html User-Agent: Mozilla/5.0 (Windows NT 6.1) HTTP/1.1 200 OK Connection: close Accept-Ranges: bytes ETag: "17000000033a66-30-47618bbfc2800" Server: Apache/2.2.25 (Win32) Content-Length: 48 Content-Type: text/html Client-Response-Num: 1 <html> <body> Hello world! </body> </html>
As you can see in the output, the enhanced script is working as I expected now.
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