Defindit Docs and Howto Home

This page last modified: Jun 06 2007
title:HTTP header examples
description:Examples of content headers for web pages and downloads
keywords:http,httpd,header,response,server,apache,download,content

Table of contents
-----------------
Simple HTTP header
Download header Content-Disposition
Debugging


Simple HTTP header
------------------

The simple http head that tells a web browser to expect a web page is
this:

Content-Type: text/html;

It must be followed by two newlines (Linux \n\n, Windows is carriage
return and linefeed I think, Mac is something else - maybe carriage
return)

I like to explicitly include the charset I'm using. Many web pages
forget this an then use special characters which don't render properly
in the web browser because the charset is not defined and the browser
chooses a default. If my HTML is in a string $output, this would be a
typical line of Perl:

   print "Content-Type: text/html; charset=iso-8859-1\n\n$output";

You must sent HTML output the web server (Apache) will get upset.  You
get an error if the outgoing text doesn't look like HTML. Apache will
return a 500 error if the output isn't HTML. The HTML can be as simple
as "<html><body><pre>text content here</pre></body></html>";

Sometimes it is handy to just spew text to the browser. Firefox is
fine with this, but some versions of IE have to call an external
application (such as Notepad) to read plain text.

   print "Content-Type: text/plain; \n\n";
   print "Hello world.\n";

If you print plain text without the proper header, you'll get a 500
error from the server. If you have text that looks like http headers
(name:value) then you might not get a 500 error, even though the
header is improperly formed.

foo: bar

In this case you might be able to print and get some of your output in
the browser without an error, although the results are unpredictable.



Download header Content-Disposition
-----------------------------------

Perl code:

    my $fn = "file_name.txt";
    my $size = length($output_string);

    # Tested and works in IE 6 and Firefox 1.5
    # The file name, file type, and size are correct.
    # The line endings are probably wrong for Windows.

    print "Content-Length: $size\n";
    print "Content-Type: text/plain;\n";
    print "Content-Disposition: attachment; filename=$fn\n";
    print "\n"; # Don't forget the second newline to end the http header.

    print "$output_string";

It is important *not* to have a semicolon (;) at the end of the size
parameter with Content-Length. If the size integer has a terminating ;
then Firefox will ignore it. I did not try a size followed by a space
followed by a semicolon.

The size parameter for Content-Disposition seems not to be
supported. In other words, the following is wrong and does not work:

    print "Content-Disposition: attachment; filename=$fn; size=$size\n";

The semicolon after filename seem optional. The filename may be
enclosed in double quotes (which in Perl have to be escaped to protect
them, and you could use a single-quoted string, but then the variables
wouldn't interpolate). 


In one test instance, this worked:	 

    print "Content-Disposition: attachment; filename=\"$fn\"\n";



Debugging
---------

Note the final print newline after the header lines have been
printed. The http head is terminted by two consecutive newlines, thus
each header line has a newline, and we print a final newline for the
double newline termination.

I debug by commenting things out, and trial and error. As far as I
know, http headers are not case sensitive. However, they are sensitive
to spelling, and some versions of some browsers (IE especially, but
also Opera) actually expect incorrectly spelled parameters (or so the
www says).

The semicolon after parameters on multiparameter lines is required.

You can view the output of your script with something like wget -S or
curl (not sure what command line arg with curl). You want a utility
that will show you the http headers (server headers) in addition to
getting the page or downloading the file. I'm not sure what to use
under Windows (the Web Developer extension for Firefox will display
response headers). OSX can use curl from the Terminal command line.