Services are entry points to your web site. A service is usually attached to a URL, and it generates a web page. There are other kinds of services, e.g., services that are identified by specific GET or POST parameters, and services representing redirections.

A service is composed of:

  • some identification data, allowing Eliom to choose which service should answer an incoming request;
  • a service handler that will generate the answer.

Service creation

Manipulation of Eliom services can be done throught the values of type Eliom_service.​service. The most common service creation function is Eliom_registration.​Html5.​register_service. It allows creating a service attached to a given path, which may expect some GET parameters, and which returns HTML5 content using the TyXML library.

Registrable service

The service creation could be split in two steps:

This chapter focuses on values of type service. See chapter Writing service handlers for more information on service registration.

Some services can be registered multiple times, with different options. This allows for example choosing between different handlers when the request is done in a particular session or protocol (HTTP or HTTPS).

Warning: in this manual, we use the term service both to denote a value of type service –that only contains some location information about a service–, or a fully registered service, that is also composed of a service handler. In case of ambiguities, we will use service –in green monotype– to designate a value of type service.

Creating link and forms

Value of type service may be used to create links and forms towards a service, using for example the function Eliom_content.​Html5.​D.​a. See chapter Creating links and forms for more information.

Unregistrable services

Some specific values of type service aren't associated to any service handlers. Such values are called unregistrable services, and represent, for example, links towards external sites, or registrable services pre-applied to some parameters. This allows use of service consistently for creating links. See chapter Unregistrable services .

Registrable services

Service identification

Eliom has a sophisticated service identification mechanism to choose the service handler to be executed –given an incoming request– with respect to many criteria:

  • the path of the requested URL,
  • the names of the (GET or POST) parameters,
  • some internal (GET or POST) parameter, added automatically,
  • the HTTP method,
  • the session the client belongs to (or client side process, or session group),
  • ...

But the user does not usually need to bother with this. Eliom abstracts this mechanism by its three main kinds of services. They differ by the subset of the criteria used to identified them:

  • Regular services are the main entry points of sites. They are identified by the path of the URL and by (GET or POST) parameters. They correspond to classical URLs, and they last forever once registered.
  • Attached coservices are services that share their location (URL) with a regular service (called fallback). They are identified by the path of the URL and a special parameter (added automatically by Eliom). They can be created dynamically. When an attached coservice is not available anymore (timeout, session closed, ...) it falls back to the corresponding regular service.
  • Non-attached coservices are coservices identified only by a special parameter, no matter what the path and the other parameters in the URL are. They are used to implement some behaviour that should not be attached to a particular URL. A link to a non-attached coservice will go to the current URL with just an additional special parameter. This is useful when you want the same link or form on several pages (for example, a login box) but you don't want to go to another URL. Non-attached coservices are often used with actions.


A path is represented in Eliom as a list of strings. For example:

["foo"; "bar"] corresponds to the URL foo/bar.
["dir"; ""] corresponds to the URL dir/ (that is: the default page of the directory dir).
The empty list [] is equivalent to [""].


  • You cannot create a service on path ["foo"] (URL foo, without slash at the end) and another on path ["foo";"bar"] (URL foo/bar), because foo cannot be both a directory and a file. Additionally, be careful not to use a path as a directory with Eliom, if it is a file for Staticmod (and vice versa).
  • ["foo";"bar"] is not equivalent to ["foo/bar"]. In the latter, the "/" will be encoded in the URL.

Service parameters

A service is partially identified by the name of its GET and POST parameters.

The parameters of services are specified using the ~get_params and ~post_params options of service creation functions. These options expect values of type Eliom_parameter.​params_type that represent the set of expected arguments with their types. They are built using combinators from the Eliom_parameter module. See chapter Service parameters for a detailled description of this module.

Type information associated to each argument allows Eliom to automatically convert the actual parameters into the corresponding OCaml types. If the parameter cannot be converted, the exception Eliom_common.​Eliom_Typing_Error is raised. The handling of such errors may be customized by providing the argument ~error_handler when registering the service.

Service scopes

By default, services and coservices are accessible to anybody (scope "site"). It is possible to restrict the scope of a service, making it available only to a session, a client side process, or a group of sessions.

To limit the scope of a service, just add the argument ~scope to the Eliom_registration.​Html5.​register function (same for the other register variants). The default scope is Eliom_common.​site_scope.

The same service can be registered with several scopes. This makes it possible, for example, to generate custom services for a specific user. Eliom will try to find the service by trying the following (in order):

  • scope "client-side process",
  • scope "session",
  • scope "group of session",
  • and finally scope "site".

GET services and POST services

Regular services, attached and non-attached coservices all come in two versions, GET or POST, corresponding to the HTTP method you want to be used to call them.

  • The GET method is intended to be used to retrieve a document from the server. The page is generated mainly according to the information contained in the URL. URLs may contain parameters (consisting of name-value pairs in the URL string), and these parameters may come from HTML forms (or not).
  • The POST method is used to send data to the server (files, for example), but also values coming from an HTML form. Data is sent in the body of the HTTP request. It is possible to use the POST method with an empty body.

In HTML, it is not possible to mix GET and POST parameters in forms, but it is possible to use a POST form with (fixed) GET parameters in the URL.

Eliom also allows the methods PUT and DELETE, which are often used to write RESTful applications (see below).

Regular services

Regular services with GET method are the main entry points of sites. They correspond to actual URLs, and they may be bookmarked. The other kinds of services (POST, coservices) are using the URL of existing regular GET services.

Regular services are created using the function Eliom_service.​App.​service or Eliom_service.​Http.​service.

POST services must be accessible even when the request was done without the POST parameters (for instance, when typing the URL in the browser, reloading, using bookmarks, ...). Hence every POST service has a fallback GET service.

POST services are created using Eliom_service.​App.​post_service or Eliom_service.​Http.​post_service.

Attached coservices

GET attached coservices are created using function Eliom_service.​App.​coservice or Eliom_service.​Http.​coservice.

Anonymous GET attached coservices are often created dynamically with respect to previous interaction with the user (e.g. filling forms in multiple steps). They handle correctly the classical web interactions ("back" button, bookmark, tab, ...): you create a new coservice each time you want to record a precise point in the interaction with the user, to be able to come back there later.

Often, they should be used with a restricted scope (see the section scope of services).

POST attached coservices are created using Eliom_service.​App.​post_coservice or Eliom_service.​Http.​post_coservice.

They can be used to customize the behaviour of an URL. Some of their usages are:

  • For the same purpose as GET coservices, but when you don't want this service to be bookmarkable.
  • For performing side effects before serving a page. For example say you want a disconnection button that leads to the main page of the site, but with the side effect of disconnecting the user. You will use a (named) POST (attached) coservice.

If a coservice does not exist anymore (e.g., if its timeout has expired, or if the session to which it belongs was closed), the fallback is called.

The fallback of a GET coservice cannot take parameters. But it is possible to use a pre-applied service as fallback.

Non-attached coservices

Non-attached coservices are coservices that are not attached to an URL path. Service identification is performed only according to the coservice identifier, whatever be the path. When you point a link or a form towards such a service, the URL path and the main parameters do not change. The parameters of the non-attached coservice are sent as special parameters.

To create a non-attached coservice, use Eliom_service.​App.​coservice' or Eliom_service.​App.​post_coservice' (note the "prime" at the end), or the same functions in Eliom_service.​Http.

Use POST non-attached coservices for example if you want a link or form to be present on every page, but you don't want the URL to change when the link is followed. Typically, non-attached POST coservices are used with actions or redirections.

Here is a simple example. Suppose you wrote a function remove to remove one piece of data from a database (taking an identifier of the data). If you want to put a link on your page to call this function and redisplay the page, just create an action on a non-attached coservices like this:

let remove_action =
    ~post_params:( "id")
    (fun () id -> remove id)

Then wherever you want to add a button to do that action (on data id), create a form like:

Html5.D.(Form.post_form remove_action
          (fun id_name -> [
               ~input_type:`Hidden ~name:id_name ~value:id
               ~input_type:`Submit ~value:("remove "^string_of_int id)

Changing URL when calling a non-attached coservice

By default, the URL of links or forms to non-attached coservices is the current page. If you want to combine the call to a non-attached coservice with a URL change, it is possible to attach a non-attached service to another service using function Eliom_service.​attach_coservice'.


let service =
a ~service [pcdata "click"] ()

It works with GET or POST coservices. The fallback must be a GET service without parameters (but you can preapply it).

Common options for coservices

Timeouts for coservices

It is possible to use timeouts with coservices, using the optional parameter ?timeout of the creation functions. For example if your coservice is intended to show the results of a search, you probably want it to be available only for a short time.

Warning: forgetting timeouts may cause memory leaks!

Disposable coservices

It is possible to set a limit to the number of uses of (attached or non-attached) coservices. Just give the maximum number of uses with the optional ?max_use parameter while creating your coservices.

Unregistrable services

External services

It is possible to define external services, that is, services that belong to an external web site (on the same server or not). To do this, use the functions Eliom_service.​Http.​external_service (for using the GET method), or Eliom_service.​Http.​external_post_service (for using the POST method).

For example, the following code defines a link to the OCaml Wikipedia page:

     ~get_params:Eliom_parameter.(suffix (all_suffix "suff"))
  [pcdata "OCaml on wikipedia"]

Static files service

Staticmod is an Ocsigen Server extension serving static (non-generated) files (for examples images and stylesheets). It can be used together with Eliom. The predefined service Eliom_service.​static_dir can be used to make links to static files. It takes as parameter the path of the file.

For example, the following code will create this link: download image.

let open Eliom_content.Html5.F in
a (static_dir ()) [pcdata "download image"] ["ocsigen10.png"]

It is also possible to send static files using Eliom, with Eliom_registration.​File (see Sending files).

Pre-applied services

It is possible to preapply the GET parameters of a service to obtain a service without parameters, or only the POST ones. It is done using Eliom_service.​preapply. Example:

let some_service =
  Eliom_service.Http.service ~path:["serv"] ()

let preappl = Eliom_service.preapply some_service 3

It is not possible to register a handler on a preapplied service, but you can use them in links or as fallbacks for coservices.

Void coservices

Eliom_service.​void_coservice' is a special non-attached action, with special behaviour: it has no parameter at all, not even non-attached parameters. Use it if you want to make a link to the current page without non-attached parameters. It is almost equivalent to a POST non-attached coservice without POST parameters, on which you register an action that does nothing, but it is using the GET method, so that you can use it with <a> links, not only with forms. Example:

a Eliom_service.void_coservice' [pcdata "cancel"] ()

There is also Eliom_service.​https_void_coservice' (same, but forces use of HTTPS), Eliom_service.​void_hidden_coservice', and Eliom_service.​https_void_hidden_coservice'. "Hidden" means that they keep GET non-attached parameters.

Use Eliom_service.​void_hidden_coservice' for example after a POST request if you want to do a redirection towards the same page without POST parameters (and thus prevent from reposting data if the user reloads the page).

RESTful apps: PUT and DELETE services

This is an example showing how to define RESTful services with Eliom. The purpose of this sample application is to provide a HTTP REST API to a very simple key / value store. Supported operations are:

  • GET http://<URL>/ List all available keys.
  • GET http://<URL>/<KEY> Retrieve the value associated to KEY.
  • POST http://<URL>/<KEY> (with <VALUE> in content) Set a new pair KEY / VALUE, or replace it if the KEY exists.
  • PUT http://<URL>/<KEY> (with <VALUE> in content) Update the VALUE associated to KEY. Fail if KEY doesn't exist.
  • DELETE http://<URL>/<KEY> (content is ignored) Delete the pair KEY / VALUE.
open Lwt
open Eliom_parameter

(* Initialize the key / value store (a simple Hashtbl) *)
let store = Hashtbl.create 100

(* Our API path is root, since the only purpose of our application is to
 * provide a sample API. *)
let path = []

let () =

  (* GET service: retrieve resources *)
  let retrieve_service =