View source with raw comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2000-2022, University of Amsterdam
    7                              CWI, Amsterdam
    8                              SWI-Prolog Solutions b.v.
    9    All rights reserved.
   10
   11    Redistribution and use in source and binary forms, with or without
   12    modification, are permitted provided that the following conditions
   13    are met:
   14
   15    1. Redistributions of source code must retain the above copyright
   16       notice, this list of conditions and the following disclaimer.
   17
   18    2. Redistributions in binary form must reproduce the above copyright
   19       notice, this list of conditions and the following disclaimer in
   20       the documentation and/or other materials provided with the
   21       distribution.
   22
   23    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   24    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   25    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   26    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   27    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   28    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   29    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   30    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   31    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   32    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   33    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   34    POSSIBILITY OF SUCH DAMAGE.
   35*/
   36
   37:- module(cgi,
   38          [ cgi_get_form/1              % -ListOf Name(Value)
   39          ]).   40
   41:- use_foreign_library(foreign(cgi)).

Read CGI parameters

Below is a very simple CGI script that prints the passed parameters. To test it, compile this program using the command below, copy it to your cgi-bin directory (or make it otherwise known as a CGI-script) and try the query http://myhost.mydomain/cgi-bin/cgidemo?hello=world

% swipl -o cgidemo --goal=main --toplevel=halt -c cgidemo.pl
:- use_module(library(cgi)).

main :-
    set_stream(current_output, encoding(utf8)),
    cgi_get_form(Arguments),
    format('Content-type: text/html; charset=UTF-8~n~n', []),
    format('<html>~n', []),
    format('<head>~n', []),
    format('<title>Simple SWI-Prolog CGI script</title>~n', []),
    format('</head>~n~n', []),
    format('<body>~n', []),
    format('<p>', []),
    print_args(Arguments),
    format('</body>~n</html>~n', []).

print_args([]).
print_args([A0|T]) :-
    A0 =.. [Name, Value],
    format('<b>~w</b>=<em>~w</em><br>~n', [Name, Value]),
    print_args(T).

*/

 cgi_get_form(-Form)
Decodes standard input and the environment variables to obtain a list of arguments passed to the CGI script. This predicate both deals with the CGI GET method as well as the POST method. If the data cannot be obtained, an existence_error exception is raised.
Arguments:
Form- is a list of Name(Value) terms.