Penn State

Web Conference 2004

Writing Perl/CGI Scripts for ITS/ASET Web services

More On CGI

<- Back - Speaking CGI| Up |POST method - Next ->

Basic requirements of a CGI program

In order for a CGI program to work correctly, the program must complete the HTTP headers. Consider the previous example.

Web Server Program (Apache) <==STDOUT-- passingdata.cgi
Content-Type: text/html

<html>
<body>
<p>Your favorite color is blue.</p>
</body>
</html>

The first line sets the content type, or MIME type, to text/html, indicating to the browser the page should be rendered as HTML. What's important to note, is not the line, but the line after. It is blank. This indicates that the HTTP headers are complete, and the following lines belong to the Web page - the HTTP body. There can be many HTTP header lines sent by the CGI program, or none, just as long as that one blank line arrives. In fact, the following Perl script code is legal cgi, just not very useful.

#!/usr/local/bin/perl

print "\n";

In the case there is no Content-Type, the Web server will use the default.

If the HTTP headers are not completed, or if there is an HTTP header line sent by the CGI program that the Web server does not understand, it will return "500 - Internal Server Error" to the browser.

<- Back - Speaking CGI| Up |POST method - Next ->

If you have any questions, feel free to ask me - mailto:jcd@psu.edu

Content by: Jeff D'Angelo <jcd@psu.edu> © 2004

Last update on: Sunday, 13-Jun-2004 17:06:57 EDT