Saltar al contenido principal
Versión: Siguiente

$singleton

You can directly call exposed functions of your shared singletons through REST.

Singleton functions are called in POST requests with the $singleton command and without (). For example, if you have defined a buildVehicle() function in the VehicleFactory shared singleton class, you could call it using the following request:

/rest/$singleton/VehicleFactory/buildVehicle

with data in the body of the POST request: ["truck"]

En el lenguaje 4D, esta llamada equivale a:

$singleton:=cs.VehicleFactory.me.buildVehicle("truck")
nota

Keep in mind that only functions with the exposed keyword can be directly called from REST requests.

Llamadas de las funciones

Singleton functions must always be called using REST POST requests (a GET request will receive an error). La sintaxis formal es:

/rest/$singleton/SingletonClass/SingletonClassFunction

All 4D code called from REST requests must be thread-safe if the project runs in compiled mode, because the REST Server always uses preemptive processes in this case (the Use preemptive process setting value is ignored by the REST Server).

info

You can restrict calls to specific singleton functions by configuring appropriate privileges in the roles.json file.

Parámetros

You can send parameters to singleton functions. On the server side, they will be received in the declared parameters of the singleton class functions.

Sending parameters to singleton functions is exactly the same as sending parameter to ORDA class functions. Please refer to the Parameters paragraph of the "Calling class functions" page for a detailed description.

Ejemplo

You have created a simple shared singleton with an exposed function:

//class mySingleton

shared singleton Class constructor()

exposed Function sayHello ($value : Text)
return "Hello "+$value

nota

The mySingleton class and sayHello function are listed when you call the $catalog command.

A continuación, puede ejecutar esta petición:

POST /rest/$singleton/mySingleton/sayHello

Cuerpo de la petición: ["John"]

Respuesta

{
"result": "Hello John"
}