Services

Introduction

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 special 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; and
  • a service handler that will generate the answer.

Services are most commonly used to create links and forms towards a service, using for example the function Eliom_content.​Html.​D.​a. See chapter Creating links and forms for more information.

Manipulation of Eliom services can be done through the values of type type Eliom_service.t (see Eliom_service_sigs.​S.​t). The service creation can be split in two steps:

  • create a value of type Eliom_service.t, most commonly by using the function Eliom_service.​create; and
  • register a service handler, e.g., using Eliom_registration.​Html.​register. The handler receives the server parameters (GET and POST) and is responsible for producing the content sent to the client.

The rest of this chapter focuses on service creation. See chapter Implementing service handlers for more information on handler implementation and registration.

Service creation

Warning: in this manual, we use the term service both to denote a value of type Eliom_service.t –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 Eliom_service.t.

The standard way to create a service is a call of the form Eliom_service.create ~meth ~path (), where

Service method

Services can respond to any of the GET, POST, PUT, and DELETE HTTP methods.

  • 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.
  • The PUT and DELETE methods are mostly used to implement RESTful applications.

The corresponding Eliom_service_sigs.​TYPES.​meth constructors are Get g, Post (g, p), Put g, and Delete g, where g and p belong in Eliom_parameter.params_type and correspond to the GET and POST parameters.

Service parameters

GET services expect only GET parameters, i.e., parameters that appear in the URL. POST, PUT, and DELETE services can also take POST parameters which are part of the body of the request.

Values of the type Eliom_parameter.params_type 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.

Services with path ("regular" services)

Services attached to paths, or more simply 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.

For creating a regular service, the Path p constructor of Eliom_service_sigs.​S.​path_option is used. The path p 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 [""].

In many cases, the URL corresponding to a POST service must be accessible even when a request is done without the POST parameters; for instance, when typing the URL in the browser, when reloading, when using bookmarks, etc. If that is the case, the programmer nee