CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Using LWP query

 



peterhall
New User

Jul 3, 2003, 10:31 PM

Post #1 of 6 (585 views)
Using LWP query Can't Post

I would enjoy a review of a script that uses LWP query for posting data, has anyone out there got something?

I am attempting to post a url with some string attached to a www server, (from the command line not using a form) I just cannot understand all the variables in LWP query.

Is it that complex? please help I'm going around in circles.

Thanx PDH


davorg
Thaumaturge / Moderator

Jul 4, 2003, 1:21 AM

Post #2 of 6 (583 views)
Re: [peterhall] Using LWP query [In reply to] Can't Post

Have you tried looking at the LWP cookbook?

perldoc lwpcook

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


peterhall
New User

Jul 4, 2003, 2:36 AM

Post #3 of 6 (582 views)
Re: [davorg] Using LWP query [In reply to] Can't Post

Yes I been there, I couldn't find anything on lwp-query though, only info on simple and the other one used for post. (slipped my mind at the moment)

I have the query module in my Activestate package, including the outlined program, but this is where I got lost. Reading through the program is beyound my skill level, I wish I could see an example of how its called and the usage.

thanks Peter.


davorg
Thaumaturge / Moderator

Jul 4, 2003, 2:57 AM

Post #4 of 6 (580 views)
Re: [peterhall] Using LWP query [In reply to] Can't Post

I'm not sure I really understand what you're asking. What do you mean by "lwp-query".

To post an query to a web server you used LWP::UserAgent and HTTP::Request (or, to make live easier, HTTP::Request::Common). There are examples of both of these in lwpcook.

Code
use HTTP::Request::Common qw(POST); 
use LWP::UserAgent;
$ua = LWP::UserAgent->new;

my $req = POST 'http://www.perl.com/cgi-bin/BugGlimpse',
[ search => 'www', errors => 0 ];

print $ua->request($req)->as_string;


The only variable parts of the request are the URL which passed as the first argument to POST and the set of parameters which are passed in the anonymous array which is the second argument.

So basically the steps are:

1/ Create an LWP::UserAgent object.
2/ Create an HTTP::Request object giving both the URL and the parameters.
3/ Use the useragent object to run the request object (using the "request" method.
4/ The returns an HTTP::Response object which tells you what was returned by the server. The example just uses the "as_string" method to display the output.

So what part fo you need more help with?

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


peterhall
New User

Jul 4, 2003, 6:32 PM

Post #5 of 6 (572 views)
Re: [davorg] Using LWP query [In reply to] Can't Post

Dave , after allot of reading on the topic I have now arrived at the same point you have shown me as a example, can you explain what we mean by

'Create an LWP::UserAgent object.' is it the '>new;' in the following lines?


use HTTP::Request::Common qw(POST);
use LWP::UserAgent;
$ua = LWP::UserAgent->new;



davorg
Thaumaturge / Moderator

Jul 5, 2003, 1:47 AM

Post #6 of 6 (569 views)
Re: [peterhall] Using LWP query [In reply to] Can't Post

Yes, that's right. It sounds that instead of trying to use rather complex objects like the LWP set you should be learning to use simpler modules and objects in Perl first. Perhaps the new book Learning Perl Objects, References & Modules would be useful to you.

Here's another copy of the code with some comments added.

Code
# Load the required modules 
use HTTP::Request::Common qw(POST);
use LWP::UserAgent;

# Create a user agent object
$ua = LWP::UserAgent->new;

# Create an HTTP POST request object giving it a URL and a
# set of parameter name/value pairs
my $req = POST 'http://www.perl.com/cgi-bin/BugGlimpse',
[ search => 'www', errors => 0 ];

# Use the user agent's "request" method to execute the request
# The value returned is a HTTP response object
my $resp = $ua->request($req)

# Use one of the HTTP Response method to do something
# useful with the response. In this case we just print it out
print $resp->as_string;


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives