How to use GET parameters (parameters in the URL)?
Pages are generated by services.
Funcrions to create services using GET HTTP method take two mandatory parameters: ~path and ~get_params.
path
This argument is a list of string, corresponding to the URL where your page service can be found.
For example, if you want your page to be accessed via this URL:
http://website.com/a/b/c
You must provide this path:
~path:["a";"b";"c"]
This parameter can also be an empty list. It means that the service page will be accessed via this URL:
http://website.com/
get_params
Eliom chooses the service automatically w.r.t. this path and parameter given (among other criteria). It decodes parameters automatically and checks their types.
Description of parameters (names and types) is done using module Eliom_parameter. We recommend to open this module (at the beginning of your file, or locally).
Syntax for several parameters
- No parameter
~get_params:unit
- One parameter
~get_params:(int "id")
- More parameters
~get_params:(string "name" ** int "age" ** bool "graduate")
Types of parameters
- Common types: string, int, int32, float, bool
- Some other types: optional values, any parameter, list, your own type.
- See more information on parameters in Eliom manual and API documentation of module Eliom_parameter.
Get these parameters
On service registration, parameters can be retrieved as a parameter of the function:
Example.register ~service:the_service
(fun parameters () -> ...)
- No parameter
(fun () () -> ...)
- One parameter
(fun a () -> ...)
- Several parameters
Operator ** is left associative:
(fun (a, (b, (c, d)))) () -> ...)
Parameters in the path
Sometimes you may want to take parameters from the path, for example to improve readability of URLs. E.g. http://blah.com/archive/2014/2/ or http://blah.com/user/toto/.
To do that, add a parameter of type suffix in get_params:
~get_params:(suffix (string "username"))
Download full code
Warning: This third party code may be outdated. Please notify the author is something is broken, or do a pull request on github.
Links
- Manual: Parameters
- Manual: Services
- API: Eliom_parameter
- API: Eliom_service