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->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();


Now repeat the same test from the previous tutorial with this new script:

herong> LWP-UserAgent-Post-Redirect.pl

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


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

Hello world!

As you can see in the output, the enhanced script is working as I expected now.

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

 Image and Picture Processing

 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

 Converting Perl Script to Executable Binary

 Managing Perl Engine and Modules on macOS

 Archived Tutorials


 Full Version in PDF/EPUB