Using POST and GET

In PERL, how can I put a GET request, get the results, and parse the results and place a POST request.

I know how to parse and all, but I need to know how to do the GET and POST things.

Tom
LVL 3
TYoungAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
clockwatcherConnect With a Mentor Commented:
Use $req->push_header($name, $value)

E.g.:

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 ];

$req->push_header(USER_AGENT => 'Mozilla/2000');
$req->push_header(REFERER => 'www.clickme.com');

# View request
print $req->as_string();

# Retrieve the page
$page = $ua->request($req)->as_string;
print $page;
0
 
TYoungAuthor Commented:
I know you have to use a module, but which one, and what functions do I have to use to do so?
0
 
ItsMeCommented:
Hi !

This is for get:

     @pairs = split(/&/, $ENV{'QUERY_STRING'});

     foreach $pair (@pairs) {
       ($name, $value) = split(/=/, $pair);
       $value =~ tr/+/ /;
       $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
       $value =~ s/~!/ ~!/g;
       $FORM{$name} = $value;
     }

and this for post:

     read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
     @pairs = split(/&/, $buffer);

     foreach $pair (@pairs) {
       ($name, $value) = split(/=/, $pair);
       $value =~ tr/+/ /;
       $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
       $value =~ s/~!/ ~!/g;
       $FORM{$name} = $value;
     }

Hope this is what you wanted to know ?
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.

 
TYoungAuthor Commented:
NO, I mean place another request on another server. My server is http://ubaddon.hypermart.net/. I want my script to call http://www.whatever.com/whatever.pl and get the results.
0
 
ItsMeCommented:
where do you want to save the results ?
0
 
ozoCommented:
use LWP::Simple;
$rc = getprint('http://www.whatever.com/whatever.pl');
print status_message($rc);
0
 
ItsMeCommented:
Hi ozo !
does your solution works with normal files, too ? please check out http://www.experts-exchange.com/jsp/qShow.jsp?ta=cgi&qid=10255159   (100 points)

thanks
ItsMe
0
 
TYoungAuthor Commented:
Thanx ozo, but how do I make a POST request?
0
 
clockwatcherCommented:
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;
0
 
TYoungAuthor Commented:
How do I add headers to the POST?
0
 
TYoungAuthor Commented:
Like User-Agent, Referer, etc.
0
 
TYoungAuthor Commented:
This works great. Thanx!
0
All Courses

From novice to tech pro — start learning today.