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

I've been sitting here the better part of a few hours trying to get a simple POST to a form to work using the LWP module. I've tried a few different ways of building the request, but no matter what I do, I get back a 400 error from apache. Here's the code I'm using now:
my $req = new HTTP::Request POST => '$sitehit'; $req->content_type('application/x-www-form-urlencoded'); $req->content('siteurl=$incoming{\'site\'}'); my $res = $ua->request($req); print "<br>Result:<br>\n"; print $res->as_string;
$sitehit is defined with a fully qualified URL to a PHP script looking for form input.

$incoming{'site'} is defined with a dummy URL just to give it something to pass to the form.

I have it printing all variables to the screen before it makes it's requests, so I know they are defined properly. Each time I run the script, I get back this:
400 (Bad Request) URL must be absolute

I know it's got to be something simple I overlooked. This is my first attempt at using LWP and the POST method. Any help at all is greatly appreciated.

Replies are listed 'Best First'.
Re: LWP Post And My Pained Brain
by LTjake (Prior) on Nov 24, 2003 at 03:02 UTC

    It's late on a sunday evening, but, items in single quotes do not interpolate.

    ex:

    $site = 'abc'; print '$site';

    output:

    $site

    HTH

    --
    "To err is human, but to really foul things up you need a computer." --Paul Ehrlich

Re: LWP Post And My Pained Brain
by pg (Canon) on Nov 24, 2003 at 03:06 UTC

    $sitehit in first line is not resolved. Use double quotes, instead of single ones (or in this case, simply don't use quotes).

    Try to run the following and observe the _uri portion:

    use Data::Dumper; use HTTP::Request; my $sitehit = "abcdefg"; my $req = new HTTP::Request POST => '$sitehit'; $req->content_type('application/x-www-form-urlencoded'); $req->content('siteurl=$incoming{\'site\'}'); print Dumper($req);

    Compare with this:

    use Data::Dumper; use HTTP::Request; my $sitehit = "abcdefg"; my $req = new HTTP::Request POST => "$sitehit"; $req->content_type('application/x-www-form-urlencoded'); $req->content('siteurl=$incoming{\'site\'}'); print Dumper($req);
Re: LWP Post And My Pained Brain
by fuzzball (Initiate) on Nov 24, 2003 at 05:01 UTC
    Thank you kindly. Seems the quotes were my problem. Just getting back into coding after quite a long break and I think I'm a bit more rusty than I had anticipated. Thanks for the help.
Re: LWP Post And My Pained Brain
by iburrell (Chaplain) on Nov 25, 2003 at 01:07 UTC
    LWP does have a helper function in HTTP::Request::Common that constructs the query string for you.
    use HTTP::Request::Common qw(POST); my $request = POST($url, [ siteurl => $incoming{site} ]);
    It also handles multipart/form-data POSTs and file uploads.