Creating REST Services and Clients with Ensemble
Walkthrough the REST Samples in ENSDEMO
[Back] [Next]

The Demo.REST.DirectoryService implements a REST service that provides access to a Caché database provided in the SAMPLES namespace.

In order to call a REST service using the CSP port, you must first define a web application to handle the class of the service. For example, to call the Demo.REST.MathService or the Demo.REST.DirectoryService in ENSDEMO, you could define a web application named /RestServices in the ENSDEMO namespace. You must allow access to the ENSDEMO database and the SAMPLES database. See Configuring Ensemble for REST Services for a walkthrough of configuring Ensemble and defining a web application.
If you are running Demo.Rest.Production, then you can enter following REST call in a web browser. This sends an HTTP GET request that queries for the people in the database whose name begins with “j”:
The browser sends this URL as an HTTP Get command. The URLMap sends this request to the retrievePerson method with the following parameters:
This method builds an SQL query statement that requests the information from the SAMPLES database and executes the query. It then calls the ObjectToJSONStream() method and returns the following JSON to the REST caller:
    "Company":"InterTron Holdings Inc.",
    "Home": {
        "Street":"1199 Maple Place",
    "Name":"Jung,Jules G.",
    "Office": {
        "Street":"803 Madison Place",
    "Spouse":"Underman,Diane Z."
    "Company":"MacroNet Associates",
    "Home": {
        "Street":"9371 Elm Drive",
    "Name":"Jung,Sam B.",
    "Office": {
        "Street":"6284 First Place",
    "Spouse":"Fripp,Hannah D."
Although you can create an HTTP GET command by just entering a URL in a browser, it is harder to generate an HTTP POST, PUT, or DELETE. You can use a utility such as curl to generate these HTTP commands or you can use the DirectoryPage and DirectoryOperation provided in the ENSDEMO REST sample. Since the DirectoryOperation calls the REST service from the Ensemble DirectoryService, it is not an ideal example showing how to call an external REST service, but it is a very useful tool to demonstrate calling the DirectoryService REST services.
To use the DirectoryPage and Directory Operation, in Studio select the ENSDEMO namespace and open the class Demo.REST.DirectoryPage. Select View Web Page and Studio displays the following web page in your default browser:
If you enter j* as the Key Value and click Retrieve, the web page and DirectoryOperation sends the same HTTP GET command that you entered in the browser. The web page displays the URL sent to the DirectoryService and the JSON returned by it. The page displays a table with two columns from the returned data. If you click on one of the rows, the page displays all of the properties and values for that person. You can then alter some of the values and click Update, which generates an HTTP POST command. If you click Delete, it deletes the record with the specified ID value with an HTTP DELETE command. Finally if you update the SSN field with a unique value and click Create, it creates a new record with an HTTP PUT command.
The DirectoryPage web page displays the JSON value returned by DirectoryService but does not display any JSON values sent to the service. One way to view these values is to use a TCP trace utility. Simply modify the DirectoryOperation configuration page to specify another port, such as 9989 and then use the TCP trace utility to transfer the message from port 9989 to 9988.
Remember, if you use this page to update the data, you are changing the data in the Caché SAMPLES database.
It is also possible to not use URLMap and implement the lower-level OnProcessInput() method. The following shows to process the call in this method:
Class Demo.REST.DirectoryService Extends EnsLib.REST.Service

Parameter ADAPTER = "EnsLib.HTTP.InboundAdapter";

/// The EnsServicePrefix parameter identifies the URL prefix that this component handles
Parameter EnsServicePrefix = "/directory";

Method OnProcessInput(
    pInput As %Library.AbstractStream,              /// Contains the HTTP command
    Output pOutput As %Stream.Object) As %Status    /// Output to return to REST call
   /// Get the HTTP operation: GET, POST, PUT, or DELETE
   Set tCmd=$ZConvert(pInput.Attributes("HttpRequest"),"U")

   /// Get the URL
   Set tURL=$ZConvert(pInput.Attributes("URL"),"I","URL")

   /// Get the 2nd part of the URL which contains the service,
   /// Test that it matches EnsServicePrefix, "/directory"
   Set tService="/"_$ZConvert($Piece(tURL,"/",2),"L")  Quit:..#EnsServicePrefix'=tService

   /// Get the 3rd part of the URL either person or employee
   Set tType=$ZConvert($Piece(tURL,"/",3),"L") 

   /// Get the 4th part of the URL in this case name
   Set tKeyIn=$Piece(tURL,"/",4), tKey=$ZConvert(tKeyIn,"L")

   /// Get the 5th part of the URL, the value to query for
   Set tKeyVal=$Replace($ZConvert($Piece(tURL,"/",5),"I","URL"),"'","''")