Perl CGI and HTTP headers

software development

#1

My Perl CGI script is returning ‘text/plain’ as the MIME type in the HTTP header returned by the Dreamhost server. This causes the Firefox browser to display my HTML code as text instead of interpreting (IE figures out that it’s HTML anyway, and displays it correctly.) I have tried setting the MIME type by using the CGI.PM module, coding as follows:

use CGI;
my $query = new CGI;
print $query->header(-type=>‘text/html’);

Unfortunately, any attempt on my part to modify the HTTP header in this way is ineffective. The server continues to return its HTTP header with the ‘text/plain’ MIME type. My specified header attribute(s) are printed after, and end up displaying with my HTML output.

I will appreciate any assistance that you can offer.

Thanks
-Mark


#2

That’s great and all, but the important part is when the CGI script is outputting the headers. Obviously it has to be the first output or its not going to work. You could be using some module or other that is has a function being called first before any code you are writing.

Generally you can test a CGI script from the command line in shell:
prompt: perl /home/username/domain/script.pl

And if using the CGI module, it might ask for paramters; just hit enter if you don’t wish to enter any. The next output from the script should be the headers you’re expecting (unless your script requires the CGI environment).

You might also try using something like:

[code]BEGIN {

output this at compile time

print “Content-Type: text/html\n\n”;
}[/code]To see if that makes the script output that as the headers.

Also, are you attempting to use Unicode or a character set that uses other than low ASCII?

:cool: Perl / MySQL / HTML CSS


#3

Thanks to all who replied, I did eventually figure
this out based on a reply to my post in
comp.lang.perl.misc. I believe that the reply
posted in this forum was also on the right track,
and was helpful.

-Mark

=====================

Thanks to everyone who replied. It appears that Alan
was on the right track. My CGI script is implemented
in two separate modules, and each module creates a
new CGI object. I was setting the header from the
second instance, which was streamed after whatever
default header was being produced by the first
instance.

I added my header code to the first instance, and
now everything works fine.

So. . .it appears that the web server will output an
HTTP header for each instance of a CGI object that
I create? Is there any way to create a CGI object
without producing an HTTP header? I am using CGI
in my first module simply to parse the URL. I would
prefer not to produce any output from the first module,
although I suppose I can live with it.

Thanks
-Mark


#4

[qutoe]Is there any way to create a CGI object
without producing an HTTP header? I am using CGI
in my first module simply to parse the URL. I would
prefer not to produce any output from the first module,
although I suppose I can live with it.[/quote]

Uh, with the standard CGI module, just don’t print CGI->header() and you’d be fine. Apparently the first module is somehow generating output, you could try looking for print statements and modify them.

:cool: Perl / MySQL / HTML+CSS