Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Set HTTP Status code

by molybtek (Novice)
on Nov 16, 2011 at 03:51 UTC ( #938304=perlquestion: print w/ replies, xml ) Need Help??
molybtek has asked for the wisdom of the Perl Monks concerning the following question:

I would like to change the status code when returning a page from perl. I'm using CGI-Application, and I tried calling $self->header_props(-status => 404);

This works on my local machine, but when I tried doing the same on my server which uses mod_perl, I get:

Not Found

The requested URL /test.pl was not found on this server.

Additionally, a 302 Found error was encountered while trying to use an ErrorDocument to handle the request.

Where test.pl was the perl script that I'm trying to change the status code from...

So, just wondering why this is the case and how can I resolve it? Thanks

Comment on Set HTTP Status code
Download Code
Re: Set HTTP Status code
by quester (Vicar) on Nov 16, 2011 at 06:30 UTC

    First check the HTTP server's access and error logs. There will probably be a message in the error log saying that test.pl was not found, but you may find a more specific reason. If not, you will have to figure out why the server can't find (or can't open) the script.

    There are several possibilities. First, check that the URL you use really corresponds to the directory where the script is. Commonly, a test.pl script would be in a cgi-bin directory and the URL needs to end in .../cgi-bin/test.pl.

    Another possibility is that test.pl is not executable by the HTTP server; an ls -l will show you if the appropriate execute bits are missing, or if the file ownership isn't what you expected.

      We are using Apache 2.2 web server.

      The server does find the code and is executing it - if I removed the code to change the status, it will display the output from the program, and the status is 200 OK.

      However, I want it to display a status of 404 Not Found, so when I included the code $self->header_props(-status => 404);, I get the error message instead of the output from the program.

      I've checked the error log, there are no entry for it.
Re: Set HTTP Status code
by Anonymous Monk on Nov 16, 2011 at 07:40 UTC
Re: Set HTTP Status code
by flexvault (Parson) on Nov 16, 2011 at 10:18 UTC

    You don't mention which web server, but if you're using a RFC compliant server, certain status codes are only valid with specific headers. I used to be able to return any status code I wanted, now I have to have the correct HTML headers in order to return the appropriate status code.

    Just a thought!

    Good Luck

    "Well done is better than well said." - Benjamin Franklin

      We are using Apache 2.2 web server

      I am able to execute the program - it is just that if I execute $self->header_props(-status => 404);, then the server doesn't output the results of the program, instead, it outputs the error message as posted.

      So I'm guessing it may have something to do with mod_perl - since I don't have that set up on my local environment, and it works.

Re: Set HTTP Status code
by aaron_baugher (Deacon) on Nov 17, 2011 at 17:26 UTC

    You're telling it to return a 404 Not Found error, and it appears to be doing that, just adding some language to it. I'm not a mod_perl expert, but could it be that mod_perl is getting the 404 header, recognizing it, and then sending it to the browser in its own words, ignoring the rest of your script's output?

    Aaron B.
    My Woefully Neglected Blog, where I occasionally mention Perl.

      This is getting weirder... Using a test script as shown below, it works with mod_perl.
      #!/usr/bin/perl use CGI qw/:standard/; header(-status => 404);
      However, if I specified something in the query string, then the status is still set as 404, however, it displays

      Not Found

      The requested URL /cgi-bin/portal/browselocation.pl was not found on this server.

      Additionally, a 302 Found error was encountered while trying to use an ErrorDocument to handle the request.

        This is getting weirder...

        So its been a few months, but you're still omitting all the critical details. Do you want help, yes or no?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://938304]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2014-12-27 04:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls