Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

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 / was not found on this server.

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

Where 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

Replies are listed 'Best First'.
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 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 script would be in a cgi-bin directory and the URL needs to end in .../cgi-bin/

    Another possibility is that 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 (Monsignor) 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 (Curate) 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/ 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://938304]
Approved by Old_Gray_Bear
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2018-02-24 06:36 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (310 votes). Check out past polls.