restServerEndpointAdd (Function) |
Top Previous Next |
Create an endpoint on the REST server.
Endpoints are used to provide services to the clients. Endpoints specifies which URLs should be mapped to different callbacks. It is possible to add endpoints to a running server, but the server must have at least one endpoint to start. A server can have up to 128 endpoints.
Note that the request provided to the callback can only be read and may not be freed or used outside the callback.
Incoming requests are limited to a maximum of 20 kB per post parameter and a max body size of 1 MB.
Multiple prioritized endpoints for similar URLs:If there is shared code that should be run for multiple requests, e.g. for validate the user, it is possible to configure it so the request calls multiple endpoints in a sequence. The url_prefix and url_format parameters are used to control which endpoints will be called depending on the URL. The variables specified in url_format will be extracted from the URL and made available as part of the query parameters. The prio parameter is used to control the priority of the endpoint, which controls the order the matching endpoints are called in. When a endpoint callback does not use 0 as return code, no more callbacks will be called for the request, skipping the endpoints with lower priority.
An example uses a server is configured to use the following endpoints:
The callbacks all return 0, so all the matching endpoints will be called. Accessing the following URLs will trigger the endpoints in the shown order:
As can be seen, endpoint 1 and 2 are called for all the requests, while e.g. endpoint 6 is only called when there are two segments after the prefix and the last segment is "4".
Input: handle : SYSHANDLE A handle to the server.
method : STRING (default "*") The HTTP method to handle with this endpoint. Typical methods are "GET", "POST "and "PUT". Use "*" to handle any method.
url_prefix : STRING This is an optional fixed part of the URL that must match the start of the request URL.
url_format : STRING The format of the endpoint URL, which can include variables that will be extracted. Separate words with "/". Prefix variables with "@" or ":". End url_format with "*" to not test the rest of the URL.
The variables can be read using restReqQueryGet.
prio : INT The priority of the endpoint. 0 is the highest priority. This is needed when multiple endpoints might match the same URL and to call multiple callbacks for the same request, to e.g. authenticate the user in one callback and then generate the data in another.
cb_func : CALLBACK Callback to call when the endpoint is called. It is called with a handle to the request from the client, a handle to an empty response to fill out and with the value of the user argument. A positive return value is used as the HTTP status code, e.g. 200 (OK) for success, 404 (Not Found) if something is not found. A return value of 0 will cause it to continue to the next callback for the same endpoint. A return value of -1 will cause it to complete the transaction and send status code 401 (Unauthorized) to the client, requesting basic authentication from the client, see restReqGetBasicAuthenthication. A return value of -2 will cause it to complete the transaction and send status code 500 (Internal Server Error) to the client. As the response can not be sent before this callback has returned, it should try to return as quickly as possible.
cb_arg : DINT User argument to pass on to the callback.
Returns: INT
Declaration: FUNCTION restServerEndpointAdd : INT;
Callback declaration: FUNCTION CALLBACK restServerCallback : INT;
Example: //----------------------------------------------------------------------------- |