Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

failing to generate proper http redirect headers

by AidanLee (Chaplain)
on Aug 29, 2003 at 19:29 UTC ( #287807=perlquestion: print w/replies, xml ) Need Help??

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

Hey monks,

What should be the easiest thing in the world is not working for some reason. I've constructed the script (shown in its entirety) below so that an end-user can run a script that is usually run as a cron job. Note that the script this script runs produces no output (ran from the command line).

The only thing apache is telling me in it's logs is "Premature end of script headers" even though the headers appear fine (as they should, not hand-rolling them myself).

#!/usr/bin/perl use CGI; my $q = new CGI; print $q->redirect(-location => "http://redirected_url"); system 'perl /path/to/ inputfile.txt';
output at the command line:
Status: 302 Moved location: http://redirected_url ---- there is a blank line here, as there should be ---
any ideas what's wrong?

Replies are listed 'Best First'.
Re: failing to generate proper http redirect headers
by isotope (Chaplain) on Aug 29, 2003 at 19:47 UTC
    This is what's wrong:
    location: http://redirected_url
    Location, like all other HTTP headers, must have the first letter capitalized. The reason it isn't is that you asked CGI to add a header called 'location' with that url as the content. Here are the examples from the CGI documentation:
    print $query->redirect('http://somewhere.else/in/movie/land'); print $query->redirect(-uri=>'http://somewhere.else/in/movie/land', -nph=>1);
    Note that uri is the correct named parameter, or the URI may be passed as the sole parameter.

      Location, like all other HTTP headers, must have the first letter capitalized.
      No, headers are completely case-insensitive. They're based on RFC822 (the format for mail messages).

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.

        Ok, my mistake. Not only did he use the redirect method incorrectly, evidently he has a non-compliant browser.

Re: failing to generate proper http redirect headers
by The Mad Hatter (Priest) on Aug 29, 2003 at 19:44 UTC
    Instead of -location, use -url:
    print $q->redirect(-url => "http://redirected_url");
    which produces
    Status: 302 Moved Location: http://redirected_url
    (Note the uppercase 'L'. I suppose you could also use -Location, but the docs say to use -url (at least the last time I looked).)

    Update As isotope pointed out, the docs now say to use -uri, though -url will still work.

      well, the depressing thing is that i've already tried all these variations mentioned:

      $q->redirect('bare url'); $q->redirect(-uri=>'url'); $q->redirect(-url=>'url'); $q->redirect(-location=>'url');

      and no successes. anyone have any other ideas? I'm beyond stumped

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://287807]
Approved by krisahoch
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2022-05-29 04:35 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (101 votes). Check out past polls.