Thomas Drought wrote:
> Hello all,
> 
> I was wondering if someone could help me. I have a script which send
> an HTTP::Request
> 
> I would like to be able to view what it is sending. I thought I could
> just create use HTTP::Daemon for this, but I'm not getting any
> response from it. I want to see the information from the initial
> request with headers. 
> 
> Here is what I have...
> 
> Request:
> 
>   $req = HTTP::Request->new(POST =>
> 'http://localhost:83/cgi-bin/server.cgi' );

OK, that creates a request, but it doesn't send it. You need to create an
LWP::UserAgent. Also, your server doesn't bind to port 83, so the request
won't get there.

> 
> And Daemon:
> 
> #!/usr/bin/perl
> 
> use vars qw( $req $d $c );
>                   ( $req, $d, $c ) = ();

"my" is preferred over "use vars" here.

> 
> 
> use strict;
> use HTTP::Daemon;
> 
> $d = HTTP::Daemon->new;

Need to bind to port 83 or change your request.

> while ( $c = $d->accept ) {
>      $req = $c->get_request;
>      printf $req

print() not printf(). Also you need $req->as_string to get meaningful
results.

>      }
> $c = undef;   # don't forget to close the socket

Your script will never get to this line.

Here's a self-contained example. The server exits after one request.

    #!/usr/bin/perl
    use strict;
    use LWP::Simple;
    use HTTP::Daemon;

    my $PORT = 8888;
    my $URL = "http://localhost:$PORT/cgi-bin/server.cgi";;

    defined(my $pid = fork) or die $!;
    unless ($pid) {
        my $d = HTTP::Daemon->new(LocalPort => $PORT, ReuseAddr => 1)
            or die "Couldn't start Daemon";
        print "Daemon ready at ", $d->url, "\n";
        my $c = $d->accept;
        my $r = $c->get_request;
        print "Request received:\n", $r->as_string, "\n";
        exit;
    }

    sleep 2;        # allow daemon to start
    get $URL;

Outputs:

Daemon ready at http://yourhost.yourdomain.com:8888/
Request received:
GET /cgi-bin/server.cgi HTTP/1.0
Host: localhost:8888
User-Agent: lwp-trivial/1.40


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to