The server-body is the code that handles the request and formulates a
reply. To facilitate all mentioned setups, the body is driven by
The goal is called with the parsed request (see
section 3.12) as argument and
set to a temporary buffer. Its task is closely related to the task of a
CGI script; it must write a header declaring holding at least the
Content-type field and a body. Here is a simple body
writing the request as an HTML table.
reply(Request) :- format('Content-type: text/html~n~n', ), format('<html>~n', ), format('<table border=1>~n'), print_request(Request), format('~n</table>~n'), format('</html>~n', ). print_request(). print_request([H|T]) :- H =.. [Name, Value], format('<tr><td>~w<td>~w~n', [Name, Value]), print_request(T).
The infrastructure recognises the header fields described below.
Other header lines are passed verbatim to the client. Typical examples
Set-Cookie and authentication headers (see section
text/*or the type matches with
UTF-8(case insensitive), the server uses UTF-8 encoding. The user may force UTF-8 encoding for arbitrary content types by adding
; charset=UTF-8to the end of the
chunkedoption in http_handler/3.
Statusheader to force a redirect response to the given URL. The message body must be empty. Handling this header is primarily intended for compatibility with the CGI conventions. Prolog code should use http_redirect/3.
Location, where Status must be one of 301 (moved), 302 (moved temporary, default) or 303 (see other).
Besides returning a page by writing it to the current output stream,
the server goal can raise an exception using throw/1
to generate special pages such as
etc. The defined exceptions are:
http_reply(not_modified,). This exception is for backward compatibility and can be used by the server to indicate the referenced resource has not been modified since it was requested last time.
In addition, the normal
"200 OK" reply status may be
overruled by writing a CGI
Status header prior to the
remainder of the message. This is particularly useful for defining REST
APIs. The following handler replies with a
handle_request(Request) :- process_data(Request, Id), % application predicate format('Status: 201~n'), format('Content-type: text/plain~n~n'), format('Created object as ~q~n', [Id]).