Page History
...
Div | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
The RESTful Web Service plugin allows exposing business logic as a RESTful Web Service.
About RESTful Web ServicesRESTful Web Services utilize the features of the HTTP Protocol to provide the API of the Web Service. For example, it uses the HTTP Request Types to indicate the type of operation:
Using these 4 HTTP Request Types a RESTful API mimics the CRUD operations (Create, Read, Update & Delete) common in transactional systems. A defining feature of REST is that it is stateless: each call the to a RESTful Web Service is completely stand-alone: it has no knowledge of previous requests. Implementing a RESTful Web Service in ServoyA RESTful Web Service endpoint can be is created by creating defining a Form in a solution Solution and implement implementing one or more of the following methods to the Form:
N/A ws_authenticate To implement custom authentication N/A | To set HTTP headers in the responsews_response_headers The RESTful Web Service endpoint is then available through the URL If the method for one of the HTTP Request Types is not implementedWith 2019.09 the {formName} can also be a scope so you don't need to create a form (thats more a ui element) If the method for one of the HTTP Request Types is not implemented, that operation is not available on the endpoint and when attempted to be called a METHOD_NOT_ALLOWED (HTTP 405) error will be thrown. The RESTful Web Service plugin does not have any client side functions or properties: all its magic happens by implementing the methods with the predefined
Keeping It StatelessRESTful Web Services are to be stateless. The RESTful Web Service plugin internally uses a pool of Headless Clients to handle multiple concurrent requests to the REStful Web Service API. As subsequent requests to the RESTful Web Service API might be handled by different headless clients in the pool of clients configured for the plugin, no state should be preserved in the headless client at the end of the call to the API. ThereforTherefore, make sure any state is cleared at the end of logic. This means at least the following:
Implement ws_read():Object to allow data retrievalBy performing an HTTP GET on the url {serverUrl}/servoy-service/rest_ws/{solutionName}/{formName},Supporting GET RequestsGET Requests are supported on the endpoint if the If the return value is a JavaScript object, it will be serialized and then placed in the body of the HTTP Response. See Returning Binary Data for more info on returning binary content. If the return value of the method is null, a NOT_FOUND response (HTTP 404) will be generated. A byte array can be returned by returning a dataprovider of type MEDIA or a file read from disk.
Implement ws_create(content):Object to allow adding dataBy performing an HTTP POST on the url {serverUrl}/servoy-service/rest_ws/{solutionName}/{formName}, the The Supporting POST RequestsPOST Requests are supported on the endpoint if the The body content of the HTTP Request is passed into the If the body content cannot be converted to a JavaScript object / string based on the Content-Type, an INTERNAL_SERVER_ERROR response (HTTP 500) will be generated. If the content in the body of the HTTP Request is missing, a NO_CONTENT response will be generated (HTTP 204). The
Implement ws_delete():Boolean to allow data removalBy performing an HTTP DELETE on the url {serverUrl}/servoy-service/rest_ws/{solutionName}/{formName}, the Implement ws_update(content):Boolean to allow updating existing dataBy performing an HTTP PUT on the url {serverUrl}/servoy-service/rest_ws/{solutionName}/{formName}, thews_update method will be invoked. Data has to be supplied in the body of the HTTP request. The method has to return a Boolean value: – true: to indicate successful update
The method can optionally return a JavaScript primitive/array/object or a byte array, from 2020.12 on you can return a JSFile object so you can stream large files to the client. If the return value is a JavaScript object, it will be serialized and then placed in the body of the HTTP Response. See Returning Binary Data for more info on returning binary content. See what other arguments you can receive in this method call. Supporting DELETE RequestsDELETE Requests are supported on the endpoint if the
Implement ws_authenticate(user, password):Object to allow authentificationIf present, it will be called before performing any rest operation (ws_read, ws_create, ws_delete, ws_update). Supporting PUT RequestsPUT Requests are supported on the endpoint if the
The body content of the HTTP Request is passed into the If the body content cannot be converted to a JavaScript object / string based on the Content-Type an INTERNAL_SERVER_ERROR response (HTTP 500) will be generated. Request parametersAdditional parameters can be specified If the content in the body of the HTTP Request is missing, a NO_CONTENT response will be generated (HTTP 204). See what other arguments you can receive in this method call.
Additional arguments - when adding Extra Data to the Request URL or return values in ws_authenticateAdditional data can be added in the URL of the HTTP Requests. There are two variations and how they are forwarded to made available in the Additional URL path elementsFragments Additional arguments URL fragments can be specified by adding added to the URL pathlike: code
The additional URL path elements fragments Query parametersParameters If the URL contains Query parameters, these will be passed into the Example Code Block | Note that if custom endpoint restrictions are implemented and the Combining Additional URL Fragments and Query Parameters
Example A HTTP Get Request on url URL The
HTTP RequestFor the POST and PUT operation (resp. The supported Content-Types are JSON (application/json), XML (application/xml) and byte array (application/binary). The Content-Type can be explicitly set in the header of the HTTP Request: Code Block | Supported Content Types & EncodingThe plugin supports the following Content Types:
Request Content-Type and Encoding The Content-Type of the HTTP Request can be explicitly set by the caller using the
Note: the charset is optional. If not specified, utf-8 will be assumed. If For requests that specify body content (POST and PUT Requests (resp.
When the Content-Type cannot be determined, an UNSUPPORTED_MEDIA_TYPE response will be generated (HTTP 415). The supplied value in the body of the HTTP request will be applied as argument to the invocation of the method. If the body Content-Type is not application/binary, it will be converted to a JavaScript object automatically. If the body content cannot be converted to a JavaScript object based on the Content-Type an INTERNAL_SERVER_ERROR response (HTTP 500) will be generated. HTTP ResponseBy default, the plugin will respond with the same Content Type as was specified in the HTTP Request. This can be overruled by specifying a different response Content-Type in the Accept header of the HTTP Request: codeResponse Content-Type and Encoding By default, the plugin will respond with the same content type as used in the HTTP Request. The client calling the RESTful Web Service can request a different response content type by specifying the desired content type for the Response by setting the The response will be encoded with the If the encoding of the response needs to be different than the request encoding, this can be specified in the HTTP Request by setting the charset value in the Accept header:
By default, the response will be encoded with the UTF-8 charset, if the response Content-Type is not application/binary with byte array as response. If the HTTP Request specified a different encoding, that will be used instead. If the encoding of the response needs to be different than the request encoding, this can be specified in the HTTP Request by setting the charset value in the Accept header:
If the response is specified to be of type byte array, the body of the response will contain the actual stream of bytes. Example This is an example of how to get a byte array response using the http plugin provided by Servoy.
Returning Binary DataThe RESTful Web Services plugin supports returning binary data in GET operations. Binary data could for example be the content of a dataprovider of type MEDIA or the contents of a file read from disk. In this case, the body of the response will contain the actual stream of bytes. Clients consuming the web service must be able to handle the binary response. When the consuming client specifies the From 2020.12 on if you return large binary data, please use a JSFile wrapper around an actual file on disk, then the bytes are just streamed directly from disk to the client over the http request without the need to read the bytes fully in memory. Setting Custom HeadersCustom headers can be set in the response by implementing the
Each key/value pair will be added as Header to the HTTP Response. Returning Custom Status CodesWhile some of the HTTP Response status codes are hardcoded in the RESTful Webservices plugin (see this documentation), it is possible to control the HTTP Status codes from within the For example, when a
For convenience purposes, all available HTTP Status Codes are also listed under the HTTP Plugin shipped with Servoy, so instead of throwing the number 404 in the previous example, a more readable way would be to throw plugins.http.HTTP_STATUS.SC_NOT_FOUND See List_of_HTTP_status_codes on Wikipedia for additional information on all status codes It is also possible to append a detailed description for the returned status code Code Block | It is also possible to set the body content of the HTTP Response with a more elaborate message to give more context to the returned status code:
Authentication/AuthorizationThe RESTful Web service plugin can be configured to check authentication/authorization. When access is denied, an UNAUTHORIZED response is generated (HTTP 401). JSONP supportThe plugin supports so-called JSONP: a variation of JSON that allows cross domain data retrieval. The JSONP variation can be invoked by added a "callback" parameter to the HTTP Request URL:
When invoked with the value "myCallback" for the "callback" parameter, the returned JSON value will be wrapped in a function call to the "myCallback" function:
Pool of ClientsTo service the requests to the RESTful Web service API, the plugin creates a pool of (headless) clients. The maximum number of clients allowed can be set using the "rest_ws_plugin_client_pool_size" property of the plugin (default value = 5). When there are more concurrent requests than the number of clients in the pool, by default the requests will wait until a client becomes available in the pool. This behavior can be altered by setting the "rest_ws_plugin_client_pool_exhausted_action" property of the plugin. The following values are supported for this property:
SamplesA sample solution is included in the Servoy distribution (servoy_sample_rest_ws.servoy), detailing how to retrieve data from the http request and to return a response. More information on RESTful Web ServicesSee the following links for more information on RESTful Web Services:
API Docs | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
HTML Table | id | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
class | servoy sSummary | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
For convenience purposes, all available HTTP Status Codes are also listed under the HTTP Plugin, so instead of throwing the number 404 in the first example, a more readable way would be to throw plugins.http.HTTP_STATUS.SC_NOT_FOUND For additional explanation of all the status codes, see List_of_HTTP_status_codes on Wikipedia. Authentication/AuthorizationBy default access to any endpoint in unrestricted. The plugin supports 2 modes of implementing Authentication and authorization.
Global Restrictions Based on Servoy SecurityBy setting the plugins server property The endpoints must be called with HTTP Basic authentication to provide a username and password. The username/password supplied in the HTTP Request is validated against the users registered in Servoy's built-in security system and additionally against group membership. Access is denied if the user does not exist or the supplied password is incorrect, or the user doesn't belong to one of the specified groups. When access is denied, an UNAUTHORIZED response is generated (HTTP 401). Custom Endpoint RestrictionsFor each endpoint custom authentication and authorization can be added by implementing the If the method returns false or null an UNAUTHORIZED response is generated (HTTP 401). Any other return value will be added as an extra query parameter with the key JSONP supportThe plugin supports so-called JSONP: a variation of JSON that allows cross domain data retrieval. The JSONP variation can be invoked by added a 'callback' parameter to the HTTP Request URL:
When invoked with the value "myCallback" for the 'callback' parameter, the returned JSON value will be wrapped in a function call to the
Versioning support{serverUrl}/servoy-service/rest_ws/v2/mysolution/myform_or_scope/ or {serverUrl}/servoy-service/rest_ws/mysolution/v2/myform_or_scope/ This is supported (the 'v' has to be there and the '2' must be a number). What is looked up is in the 'myform_v2' form or the 'scope_v2' scope (for version 2 of the api). (supported since 2019.09) Nested call support{serverUrl}/servoy-service/rest_ws/mysolution/myscope/mymethod/mymethod2/arg1 The plugin will now lookup in 'mysolution' on the 'myscope' scope a method named 'ws_read_mymethod_mymethod2'; if that one is found it will call it with 'arg1' as an argument. If 'ws_read_mymethod_mymethod2' method is not found it will try: 'ws_read_mymethod' (that will then get 2 arguments: 'mymethod2' and 'arg1') and if that one is not found either it will fallback to 'ws_read' (with 3 arguments). Read more about method arguments. Pool of ClientsTo service the requests to the RESTful Web service API, the plugin creates a pool of (headless) clients. The maximum number of clients allowed can be set using the When there are more concurrent requests than the number of clients in the pool, by default the requests will wait until a client becomes available in the pool. This behavior can be altered by setting the
Client pluginCode running inside a rest-ws request can access the request and the response using the rest_ws client plugin. For example, retrieve a custom header sent by the client:
Note that the client-plugin can only be used within a request handler, the function See API documentation for the plugin. Sample SolutionA sample solution is included in the Servoy distribution (servoy_sample_rest_ws.servoy), detailing how to retrieve data from the HTTP Request and to return a response. More information on RESTful Web ServicesSee the following links for more information on RESTful Web Services:
API Docs |
HTML Table | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|