Common Gateway Interface (CGI)

Common Gateway Interface (CGI) is a set of standards that defines how information is exchanged between the web server and a custom script.It’s simply a basic way for information to be passed from the Web server about your request to the application program and back again.

Manvi_Singh
4 min readApr 15, 2020

CGI is an interface specification for Web servers to execute programs like console applications running on a server that generates web pages dynamically. Such programs are known as CGI scripts or simply CGIs. The CGI specs are currently maintained by the NCSA.

Web Browsing

To understand the concept of CGI, let us see what happens when we click a hyper link to browse a particular web page or URL.

  • Your browser contacts the HTTP web server and demands for the URL, ie.,filename.
  • Web Server parses the URL and looks for the filename. If it finds the file then sends it back to the browser, otherwise sends an error message indicating that you requested a wrong file.
  • Web Browser takes response from Web server and displays either the received file or error message.

However it’s possible to set up the HTTP server so that whenever a file in a certain directory is requested that file is not sent back;instead it’s executed as a program, and whatever that program outputs is sent back for your browser to display.This function is called CGI and the program is called CGI scripts.

CGI Architecture Diagram

First CGI Program

This produces the following output:-

Hello world! This is my first CGI program

This simple python script,which writes it output on STDOUT file, ie., Screen. There is one important and extra feature available which is first line to be printed Content-type:text/html\r\n\r\n . This line is sent back to the browser and it specifies the content type to be displayed on the browse screen.

GET and POST Methods

You must have come across many situations when you need to pass some information from your browser to web server and ultimately to your CGI Program.The web browser communicates with the web server typically using one of the two HTTP methods--GET and POST.

The GET Method

In GET method the data is sent as URL parameters that are usually strings of name and value pairs separated by ampersands (&).The page and the ancoded information are separated by the ? character as follows —

The bold parts in the URL are the GET parameters and the italic part are the value of those parameters. Never use GET method if you have password or other sensitive information to pass to the server. The GET method has size limitation:only 1024 characters can be sent in a request string.

You can pass information by simply concatenating key and value pairs along with any URL or you can use HTML<FORM> tags to pass information using GET method.

The POST Method

In POST method the data is sent to the server as a package in a separate communication with the processing script .Data sent with the post will not be visible in the URL. This is more reliable method of passing information to a CGI program . This packages the information in exactly the same way as GET methods,but instead of sending it as a text string after a?in the URL it sends it as a separate message. This message comes into the CGI script in tge form of the standard input.

Example of POST

The data send by a form could appear like

Name=AI+Alena&Age=23&Gender=female.

The program handles the data by partioning the data.The form data could be encoded directly using ENCTYPE attribute in the POST method .

Difference between GET and POST Method

--

--