LWP, Post, return data ?

S

still me

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
 
P

Paul Lalli

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
 
S

still me

$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!
 
S

still me

#!/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,
 
P

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.

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
 
C

comp.llang.perl.moderated

...


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")
 
S

still me

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top