CGI programming with Octave


Jump to: navigation, search



With the octave's package manager, you can install this software by issuing in an octave session the following command:

pkg install -forge -auto cgi

You can also get the lasted code version by:

svn checkout svn:// cgi

Add the inst folder to your path:


Using CGI

The cgi package uses an object-oriented interface. First it is necessary to create a CGI objects which captures the current CGI environnement:

CGI = cgi();

The CGI query string can be accessed by getfrist (returns only the value of the first parameter with a given name) and getlist (returns all values of the parameters with a given name). For example, consider the URL

var = getfirst(CGI,'name'); % returns 'foo'
vars = getlist(CGI,'name'); % returns {'foo','bar'}

Instead of getfirst, you can also use the following syntax:

var =;

More documentation is available at:

Simple script

Create a file with the following content in your web-servers cgi-bin directory (the default in Debian/Ubuntu is /usr/lib/cgi-bin). Name the file for example hello.cgi.

#!/usr/local/bin/octave -qH
printf('Content-type: text/html\n\n');

CGI = cgi();
name = getfirst(CGI,'name','world'); % 'world' is the default parameter
printf('Hello %s',name);
printf('<form action="/cgi-bin/hello.cgi" method="get">');
printf('Your name<input type="text" name="name">');
printf('<input type="submit"></form>');

The first line should be path to your octave binary. You must use the -q option for quite. This file should be made executable:

sudo chmod +x hello.cgi

Open the URL with your web-browser Of course it is extremely tedious to generate HTML from octave. It is better to separate the HTML code and the octave code. The octave CGI script would for example return data in JSON and the HTML page is updated with JavaScript. The toolbox jsonlab can be used to generate JSON from octave.


  • Only a CONTENT_TYPE "application/x-www-form-urlencoded" is supported for POST requests.
  • No file uploads
Personal tools