Search code examples
perlinternal-server-error

Internal Server Error 500 thrown from perl script -but script works fine


I have a perl script which is called via Ajax. It simply writes 3 values to a database. The code works fine (values get written successfully) but I get an "Internal Server Error" thrown. The Errorlog says "premature end of script headers".

There was no problem with the application - it works as required and has for a few months - but I noticed the error via Firebug when testing something else.

So I started stripping perl out of the script in an attempt to locate the problem .. and continued till I only had only had two lines left .. the shebang and exit .. I still get the 500 error. Running the script direct from a browser gives the 500 error in the browser window ... from the command prompt it's fine - ie. nothing in the apache errorlog.

There is nothing wrong with the server configuration - it has hundreds of perl scripts and has been running for years.


Solution

  • If you're writing to the database and don't need to send anything back to the client the proper response is status 204:

    print "Status: 204 No Response\n\n";
    

    This makes the server happy because it's sending a complete header set, and tells the client side that the request was successful and that there is no response body to process.