LWP, Post, return data ?

Discussion in 'Perl Misc' started by still me, Oct 12, 2007.

  1. still me

    still me Guest

    I am trying to write a cgi that calls (POSTs to) another cgi. The
    second cgi returns a web page (HTML). I can't seem to figure out how
    to get to the data that the second cgi sends back. The cgi runs, the
    POST works, but i don't know how to access the return output from the
    second cgi (so I can pass it back to the browser that made the initial
    call).


    Here's the (very trimmed) code of the first program:

    #!/usr/bin/perl -w
    use strict;
    use CGI qw:)all);
    use CGI::Carp qw(fatalsToBrowser);
    use LWP;
    my $browser = LWP::UserAgent->new;
    my $url = 'http://www.example.com/cgi-bin/second.pl';
    my $response = $browser->post( $url,
    [ 'ed' => '001',
    'test' => 'it worked',
    ]
    );

    # and right here I want to forward the data to the calling browser

    Thanks
     
    still me, Oct 12, 2007
    #1
    1. Advertising

  2. still me

    Paul Lalli Guest

    On Oct 11, 10:26 pm, still me <> wrote:
    > I am trying to write a cgi that calls (POSTs to) another cgi. The
    > second cgi returns a web page (HTML). I can't seem to figure out how
    > to get to the data that the second cgi sends back. The cgi runs, the
    > POST works, but i don't know how to access the return output from the
    > second cgi (so I can pass it back to the browser that made the initial
    > call).
    >
    > Here's the (very trimmed) code of the first program:
    >
    > #!/usr/bin/perl -w
    > use strict;
    > use CGI qw:)all);
    > use CGI::Carp qw(fatalsToBrowser);
    > use LWP;
    > my $browser = LWP::UserAgent->new;
    > my $url = 'http://www.example.com/cgi-bin/second.pl';
    > my $response = $browser->post( $url,
    > [ 'ed' => '001',
    > 'test' => 'it worked',
    > ]
    > );
    >
    > # and right here I want to forward the data to the calling browser


    LWP::UserAgent::post() returns an HTTP::Response object.

    http://search.cpan.org/~gaas/libwww-perl-5.808/lib/HTTP/Response.pm

    $response = $ua->request($request)
    if ($response->is_success) {
    print $response->content;
    }
    else {
    print STDERR $response->status_line, "\n";
    }

    Paul Lalli
     
    Paul Lalli, Oct 12, 2007
    #2
    1. Advertising

  3. still me

    still me Guest

    On Thu, 11 Oct 2007 19:38:27 -0700, Paul Lalli <>
    wrote:

    > $response = $ua->request($request)
    > if ($response->is_success) {
    > print $response->content;
    > }
    > else {
    > print STDERR $response->status_line, "\n";
    > }



    Thanks. I tried something like that multiple ways but it didn't work.
    But, since you insisted, I went back to it one more time. Turned out
    is was an error in terms of what was coming back from the called
    program. It's always sumthin!
     
    still me, Oct 12, 2007
    #3
  4. still me

    still me Guest

    On Thu, 11 Oct 2007 19:38:27 -0700, Paul Lalli <>
    wrote:

    >> #!/usr/bin/perl -w
    >> use strict;
    >> use CGI qw:)all);
    >> use CGI::Carp qw(fatalsToBrowser);
    >> use LWP;
    >> my $browser = LWP::UserAgent->new;
    >> my $url = 'http://www.example.com/cgi-bin/second.pl';
    >> my $response = $browser->post( $url,
    >> [ 'ed' => '001',
    >> 'test' => 'it worked',
    >> ]
    >> );
    >>
    >> # and right here I want to forward the data to the calling browser

    >
    >LWP::UserAgent::post() returns an HTTP::Response object.
    >
    >http://search.cpan.org/~gaas/libwww-perl-5.808/lib/HTTP/Response.pm
    >
    > $response = $ua->request($request)
    > if ($response->is_success) {
    > print $response->content;
    > }
    > else {
    > print STDERR $response->status_line, "\n";
    > }
    >
    >Paul Lalli


    I need a little more help. I also need to access the return headers
    that are sent back from the called cgi. I read this:

    http://search.cpan.org/~gaas/libwww-perl-5.808/lib/HTTP/Headers.pm

    And I think I need to access this: $h->as_string( $eol )

    But I am having trouble making the logical jump as to how the code
    would actually look. It's clear enough when I want to create a new
    header object, but how do I get the return values from the "$response
    = $ua->request($request)" to populate the header object? Some detailed
    code would be very helpful.

    Thanks,
     
    still me, Oct 12, 2007
    #4
  5. still me

    Paul Lalli Guest

    On Oct 12, 12:00 am, still me <> wrote:
    > On Thu, 11 Oct 2007 19:38:27 -0700, Paul Lalli <>
    > wrote:


    > >LWP::UserAgent::post() returns an HTTP::Response object.

    >
    > >http://search.cpan.org/~gaas/libwww-perl-5.808/lib/HTTP/Response.pm



    > I need a little more help. I also need to access the return headers
    > that are sent back from the called cgi. I read this:
    >
    > http://search.cpan.org/~gaas/libwww-perl-5.808/lib/HTTP/Headers.pm
    >
    > And I think I need to access this: $h->as_string( $eol )
    >
    > But I am having trouble making the logical jump as to how the code
    > would actually look. It's clear enough when I want to create a new
    > header object, but how do I get the return values from
    > the "$response = $ua->request($request)" to populate the header
    > object? Some detailed code would be very helpful.


    Again, from that same URL as above:

    "HTTP::Response is a subclass of HTTP::Message and therefore inherits
    its methods."

    Therefore, we now look at: http://search.cpan.org/~gaas/libwww-perl-5.808/lib/HTTP/Message.pm

    which shows us some available methods, such as:
    $mess->headers
    Returns the embedded HTTP::Headers object.

    $mess->headers_as_string
    $mess->headers_as_string( $eol )
    Call the as_string() method for the headers in the message.
    This will be the same as

    $mess->headers->as_string

    but it will make your program a whole character shorter :)


    Paul Lalli
     
    Paul Lalli, Oct 12, 2007
    #5
  6. On Oct 11, 9:00 pm, still me <> wrote:
    > On Thu, 11 Oct 2007 19:38:27 -0700, Paul Lalli <>
    > wrote:
    >
    > ...


    > >LWP::UserAgent::post() returns an HTTP::Response object.

    >
    > >http://search.cpan.org/~gaas/libwww-perl-5.808/lib/HTTP/Response.pm

    >
    > > $response = $ua->request($request)
    > > if ($response->is_success) {
    > > print $response->content;
    > > }
    > > else {
    > > print STDERR $response->status_line, "\n";
    > > }

    >
    > >Paul Lalli

    >
    > I need a little more help. I also need to access the return headers
    > that are sent back from the called cgi. I read this:
    >
    > http://search.cpan.org/~gaas/libwww-perl-5.808/lib/HTTP/Headers.pm
    >
    > And I think I need to access this: $h->as_string( $eol )
    >
    > But I am having trouble making the logical jump as to how the code
    > would actually look. It's clear enough when I want to create a new
    > header object, but how do I get the return values from the "$response
    > = $ua->request($request)" to populate the header object? Some detailed
    > code would be very helpful.
    >


    print $response->headers_as_string;

    * or, to pull a specific header:
    eg, print $response->header("Client-Peer")

    --
    Charles DeRykus
     
    comp.llang.perl.moderated, Oct 12, 2007
    #6
  7. still me

    still me Guest

    On Fri, 12 Oct 2007 05:22:27 -0700, Paul Lalli <>
    wrote:


    >Again, from that same URL as above:
    >
    >"HTTP::Response is a subclass of HTTP::Message and therefore inherits
    >its methods."
    >
    >Therefore, we now look at: http://search.cpan.org/~gaas/libwww-perl-5.808/lib/HTTP/Message.pm
    >
    >which shows us some available methods, such as:
    >$mess->headers
    > Returns the embedded HTTP::Headers object.
    >
    >$mess->headers_as_string
    >$mess->headers_as_string( $eol )
    > Call the as_string() method for the headers in the message.
    > This will be the same as
    >
    > $mess->headers->as_string
    >
    > but it will make your program a whole character shorter :)
    >
    >
    >Paul Lalli



    Thanks Paul. I was getting a bit confused amongst all the objects.
     
    still me, Oct 13, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. La Jesus
    Replies:
    9
    Views:
    1,337
    Gunnar Hjalmarsson
    Oct 27, 2003
  2. Karra
    Replies:
    2
    Views:
    782
    Octavian Rasnita
    Dec 29, 2010
  3. Andrew
    Replies:
    1
    Views:
    380
    Gunnar Hjalmarsson
    Dec 5, 2003
  4. Zapanaz
    Replies:
    4
    Views:
    141
    Tad McClellan
    Oct 12, 2005
  5. schruthensis
    Replies:
    18
    Views:
    819
    Matt Garrish
    Jan 7, 2006
Loading...

Share This Page