FORMs , GET requests.....

Hi,

Guess this is pretty easy if you know it....but I don't, so here goes.

All I want to know how to do is to submit a form from a Perl script. I looked at the chapter in Perl Cookbook, but couldn't quite figure out what I was meant to do. The code reads:use

LWP::Simple;
use URI::URL;

my $url = url('http://somewhere.com/index.html');
$url->query_form(module => 'DB_File', readme => 1);
$content = get($url);

How do I put form values into the URL, and how do I tell it where to be submitted to etc, etc. If you can explain what the 'DB_File' bit is above then that would be good too.

So, I want to submit a form to (for example) http://submit-here.com/submitme.cgi and I have a url of http://mydomain.com/index.html and say a couple of form values too, such as e-mail address and name - an expanded explanation would be very appreciated. Thx

D
LVL 1
dgb001Asked:
Who is Participating?
 
clockwatcherConnect With a Mentor Commented:
Thanks
0
 
clockwatcherCommented:
Which method are you looking for (a POST or a GET)?

Here's a POST:

use HTTP::Request::Common qw(POST);
use LWP::UserAgent;

$ua = new LWP::UserAgent;

# Post parameters
$myparams{"ta"} = "35";
$myparams{"search"} = "LWP";

# Create request
my $req = POST 'http://www.experts-exchange.com/bin/SimpleSearch', [ %myparams ];

# Retrieve the page
$page = $ua->request($req)->as_string;

print $page;

----------------------------------------

Here's a GET:

use LWP::Simple;
$page = get("http://www.experts-exchange.com?ta=35&search=LWP")

print $page;
0
 
clockwatcherCommented:
Sorry the get is missing a semicolon. :-(

use LWP::Simple;
$page = get("http://www.experts-exchange.com?ta=35&search=LWP");
print $page;
0
[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

 
dgb001Author Commented:
It's a GET request. I think the bit that is kinda confusing me is how to escape the form variables - I want to be able to pass the url to some routine or something and have it sort out the url - using something like URI::Escape::uri_escape etc, but I haven't the faintest idea how to do that, that's why I really want someone to explain the whole line in the question with DBFILE etc, etc, because I can see that it has something to do with it. Hope you can enlighten me a little further.

Many thx for the help so far,
D
0
 
clockwatcherCommented:
Either you're making this harder than it is or I don't quite understand your question.

The line in question:

  $url->query_form(module => 'DB_File', readme => 1);

Calls the query_form method of your $url object.  The query_form method escapes the list passed to it and appends it to the base url-- in other words it creates the url's querystring.

The following four lines:

 use URI::URL;
 my $url = url('http://somewhere.com/index.html');
 $url->query_form(module => 'DB_File', readme => 1);
 print "$url\n"

Creates the following URL and prints it to the screen:
 
  http://somewhere.com/index.html?module=DB_File&readme=1


You asked how to submit a form to http://submit-here.com/submitme.cgi with a couple of variables: email and name.

# Create the base url
 
 use URI::URL;
 my $url = url('http://submit-here.com/submitme.cgi');

# Add the querystring with a couple of variables (email and name)
 $url->query_form('email' => 'jdoe@somewhere.com', 'name' => 'John Doe');

# Request the page
 use LWP::Simple;
 $page = get($url);

The code above will retrieve data from the following URL:

  http://submit-here.com/submitme.cgi?email=jdoe%40somewhere.com&name=John%20Doe
0
 
dgb001Author Commented:
I understand now.....thanks very much. If you post it as an answer I can give you the points. I was just being a bit slow in the head. :-)

Thx,
D
0
 
dgb001Author Commented:
Sorry clockwatcher, I should really have given you an "A" when I accepted the answer that you gave, you definitely explained it very well to me - apologies, hope I can make it up next time.

D
0
All Courses

From novice to tech pro — start learning today.