Why is my CGI header being printed as part of the body?

I have a weird problem, maybe something in common with Apache than Perl. In my code, when I write:

print "content-type: text/html\n\n"; 

      

it prints this along with the code. But when I type:

$c = new CGI; $c->header(); 

      

it works fine and displays the rendered HTML output.

What is the problem?

thanks

0


a source to share


2 answers


Be aware that the HTTP RFC indicates that

\r\n

      

used for line separators, not

\n

      

so that you want to radiate



print "Content-Type: text/html\r\n\r\n";

      

Instead.

You should just use ->headers

, although you need to for that.

Also, please note that I used Camel-Case instead of lower case. While both should work, Camel-Case is the notation used in the spec, so it is preferable and more likely to work on weird UAs.

+1


a source


Is "content-type: text / html" the very first version? Use wget or similar to check the actual output; don't trust your eyes or the browser's viewing source.

Also note that in mod_perl the CGI calls the send_cgi_header method of the request instead of just printing the headers.



Is it possible that you are using the CGI () header in multiple places? This is harmless, but replacing just one call to header () with an explicit header print will give the results you see.

+2


a source







All Articles