toadi has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I'm puzzeled...
I have a form with text to translate:
<INPUT TYPE=TEXT NAME="text" SIZE=50>

So here I put a sentence to translate...
url looks like: http://somecompany.com/srcipt.dll?text=some text

now this returns just the raw tranlation in my browser with no html. Now I want to get the result and parse it in my lay-out.

For the moment I'm getting the input from the form with CGI and I'm trying to get the result with LWP.

Are There better methods(I'm not even sure this works with LWP)?

Replies are listed 'Best First'.
Re: Post and get
by lhoward (Vicar) on Jul 03, 2000 at 17:17 UTC
    Encoding URLs yourself can be a pain. Check out the URI::URL module, it does a great job of encoding URLs and it plays nicely with LWP...
    use LWP::UserAgent; use URI::URL; my $WWWAgent = new LWP::UserAgent(); my $site=new URI::URL 'http://somecompany.com/srcipt.dll'; $site->query_form( text => 'some text'); my $WWWRequest = new HTTP::Request 'GET', $site->as_string() ; my $WWWResult = $WWWAgent->request($WWWRequest); die "Error submitting form $WWWResult->code $WWWResult->message" if(!$WWWResult->is_success);
    The HTML::Parser module provides an excelent framework form parsing web pages and it works well for parsing the documents returned by LWP.
Re: Post and get
by toadi (Chaplain) on Jul 03, 2000 at 18:52 UTC
    Wel,

    I adopted Lhowards code and took mine and adjusted a bit ...
    my $WWWAgent = new LWP::UserAgent(); my $site=new URI::URL 'http://translate/lhsl.com/greenacres.dll?text=$ +text&pair=$pair&domain=$domain&interface=$interface'; # Create a request my $WWWRequest = new HTTP::Request 'GET',$site->as_string(); # Pass request to the user agent and get a response back my $WWWResult = $WWWAgent->request($WWWRequest);


    Well got the same problem with my code and this code.
    When I use cgi.pm to display $WWWResult I get ; HTTP::Response=HASH(0x833dd4c)


    This error(in the error.log from apache) I get when using completely Lhowards code:
    Error submitting form HTTP::Response=HASH(0x82bd790)->code HTTP::Response=HASH(0x82bd790)->message at /home/geert/www/cgi-bin/translate.cgi line 31.


    --
    My opinions may have changed,
    but not the fact that I am right

      I see 2 problems with your adapted version of my example:

      1. You shoul use query_form to encode the request as I did in my example. If you do not, you will have to worry about handling special characters (like spaces) in the request yourself.

      my $site=new URI::URL 'http://translate/lhsl.com/greenacres.dll';
      $site->query_form(text=>$text, pair =>$pair, domain=>$domain, interface=>$interface);

      2. $WWWresult is not the text returned by LWP; it is an HTTP::Response object. The object contains the page conent (in $WWWresponse->content) in addition to any LWP errors, etc...

        Thanx...

        That's why I come to PM. I'm always browsing CPAN, but not always understanding the manuals or overlookings something and then there are people at PM who can guide me and see the light.

        My problem is that sometimes I don't understand objects good enough to know exectly what I'm doing :-)

        But I'm learning!!!!
        --
        My opinions may have changed,
        but not the fact that I am right