Open Chinstrap

April 3, 2008

Making a POST with HTTP::Lite

Filed under: Linux, Perl — t-roy @ 7:45 am

HTTP::Lite is a Perl module that, and as the name implies, it is a lightweight implementation for HTTP support. I wanted to write a program to automate a GET or POST to a website. For example you may want to have a script that runs and logs onto a site with your username and password and then grabs some information like stock or weather data. This data could then be used in a report or loaded into a database.

To try this out you can use any website that allows you to fill out a form. I used my account at www.campfirenow.com . Campfirenow is a web service which provides online chat. You can sign up for a free user account if you want to try it out but any website with a form will work for this example. Make sure that HTTP::Lite is installed on your system:

perl -MHTTP::Lite -e 1

If this does not return an error then the module is installed otherwise you can view this post if you do not know how to install it . Before we dive into the code here is a quick check list for the program

  • Import the HTTP::Lite module
  • Create a hash for the input values in the form (its a good idea to use a simple form)
  • POST the form values to the webpage
  • Display the response from the web site (Display the web page)

# Import the module
use HTTP::Lite;

# Create a hash for the form
# Input: email_address
# Input: password

%login = (
“email_address” => “myemail\@domain.com”,
“password” => “mypassword”
);

$http = new HTTP::Lite;

# Create the POST for the form

$http->prepare_post(\%login);

$req = $http->request(”http://myaccount.campfirenow.com/login”)
or die “Error retrieving URL: $!”;
print “req: $req\n”;

# Display the web page (response)

print $http->body() , “\n”;

Unfortunatley the output from this example is not too exciting. Once the user logs in they are redirected to http://myaccount.campfirenow.com. So the HTML that is displayed is for the redirect. Secondly in order to make additional request to the web site a session_id cookie must be passed for all future requests. In my next post I will discuss how to get and set a cookie from the header using the HTTP::Lite module.

This process is more involved. First I will have to login to the site and then retrieve the session cookie from the response header. When I make another request I will have to set the cookie in the request header or the request will fail (it results in a redirect or a 302 STATUS) . This will allow me to extract data from other web pages on the campfirenow web site.

Troy

No Comments Yet »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.