![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to send a Head Http request from command line | darkling235 | UNIX for Dummies Questions & Answers | 3 | 04-03-2008 11:48 PM |
| HTTP request | noufal | UNIX for Dummies Questions & Answers | 3 | 01-31-2008 01:55 AM |
| http request forward | piltrafa | UNIX for Dummies Questions & Answers | 4 | 12-06-2007 09:40 AM |
| Http request in Linux | malaysoul | Shell Programming and Scripting | 7 | 01-26-2007 08:25 AM |
| unix script http request | arksal | UNIX for Dummies Questions & Answers | 1 | 08-24-2006 07:15 PM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
HTTP Query Request & Parsing returned XML output
I have a PERL script from which I need to make a HTTP request to Web Servlet (Essentially a URL with variables and values like &Variable1=AAAAAA&Variable2=BBBBBBBBB&Variable3=CCCCCCC). The Web servlet returns an XML result which needs to be parsed for the contents of the result within the program.
Here is what I need to know 1) How to make this http request from PERL (What modules would I require for this request) and 2) How do I read the results from the XML results that the query returns? Any help and or suggestions provided is greatly appreciated. Thank you. |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
(1) You can always construct the query string from the parameters manually. It's just a matter of 'key=value' pairs with '&' in between (as you have mentioned). If you have the list of key-value pairs ready, this should be easy but you need to watch out that non-alphanumeric characters should be escaped by the %XY notation, where XY is the hex. representation of the character's ASCII value.
Or, you can use the modules URI and URI::QueryParam which I think is safer. Code:
use URI;
use URI::QueryParam;
$u = new URI("http://someserver.com/control/servlet");
%aryParams = (
'test1' => 'value1',
'test2' => '$abc#def',
);
foreach (keys %aryParams) {
$u->query_param($_, $aryParams{$_});
}
print $u->as_string() . "\n";
Code:
http://someserver.com/control/servlet?test1=value1&test2=%24abc%23def Finally, use LWP::Simple to fire the HTTP request. (2) For reading XML files, I have previously tried using XML::Simple. It builds a data structure corresponding to the XML file. Suitable for small XML files that are not so complicated but it is quite convenient. A more complex alternative is XML::Parser, with a more-or-less SAX-like API. I haven't used this before in practice. And there are many other choices of XML parsing modules available on CPAN. Go to search.cpan.org and have a look. I have recently worked on a PHP project with lots of reading and processing from XML files. I decided to use XPath extensively and found that I required far less code with XPath than conventional "tree-walk" mechanisms. You can extract an arbitrary node anywhere in the document with some precise conditions specified just as SQL in database queries, which is handy for more complex analysis. In Perl, an equivalent module is XML::XPath. You need to download most of these modules as they are not in the core distribution. |
|
#3
|
|||
|
|||
|
Thank you for the guidance and update cbkihong. We are using AIX 5.2.0 build and the sys admin says that he has installed all optional modules for PERL.
In your post you indicate three packages. URI, LWP and XML. I think I have URI::QueryParam; LWP::Simple; XML::Simple; As indicated I do not have XML::Parser and XML::XPath. Can you please suggest a link on cpan where I might find these or do I need to go to IBM to obtain the above. Also can any suggested reading material for XML::XPath and XML::Parser either in book form or web-sites? Thanks again. Jerardfjay Last edited by jerardfjay; 05-10-2005 at 04:23 AM. |
|
#4
|
||||
|
||||
|
A search of http://search.cpan.org/ will give you what you need - I've just checked and they're there...
Cheers ZB |
|
#5
|
|||
|
|||
|
Honestly speaking, to my dismay I have found some XML modules for Perl are not that mature, still (the ones I mentioned are okay). Some only implement a part of the W3C standards (though it may be actually quite usable by most people). For many modules, the documentation are not well-written and some may even mislead you into antiquated material. So I tend to go Google for bits and pieces. But I think if you have an idea of the specs, you will find that the API for these XML technologies for most programming languages are highly familiar, so resources written for other languages may also be useful. Just try the examples you can find, bits and pieces, from the Web and you will soon find it easy to follow.
I think you will find this sample chapter excerpt useful: http://www.webreference.com/programm...perlxml/chap3/ While I did my PHP project at work I read directly the W3C spec for XPath. But that is not any pleasurable reading after all. I also read the relevant chapters of the J2EE Tutorial for info (it's for Java, of course). For a simple introduction, you may go to w3schools.com. For modules installation, modules on CPAN can be downloaded and installed with a simple (single!) command on the command line! A past poster here has written a short tutorial for that: PERL & CPAN Intro for Newbies Refer him/her here to take a look. |
|
#6
|
|||
|
|||
|
Thank you all for the wonderful insights into the world of PERL. I am still learning new stuff everyday. This makes this site truly wonderful learning experience. Thanks again.
Jerardfjay |
|
#7
|
|||
|
|||
|
Quote:
http://www.perl.com/pub/a/2000/06/27/perlbook.html an active Perl community member wrote that Quote:
|
|||
| Google The UNIX and Linux Forums |