zaterdag 3 maart 2012

Build a RESTful webservice in 3 lines of code

After building both SOAP and RESTful webservices in Uniface my opinion is: use REST when you can, SOAP when you must. This article explains why: REST vs. SOAP – The Right WebService

Not many people know how easy it is to build RESTful webservices in Uniface. Because you do not need special statements or features there is no information on it in the documentation.

Here are the steps for building a (very) basic RESTful webservice:

  1. Create a new DSP component RESTBLOG
  2. Remove all code from all triggers (especially Set State)
  3. Add a dummy entity
  4. Enter this sample code into the Execute trigger:

    public web
    variables

       string vInput  ; Input parameter from URL
       string vOutput ; Output content to be returned
    endvariables

       ; --- 1 --- get input parameter
       getitem/id vInput, $webinfo("INPUT"), "name"

       ; --- 2 --- create output based on input
       ; you can do anything here
       vOutput = "Hello %%vInput%%%, I am the output"

       ; --- 3 --- fill the output channel
       $webinfo("output") = vOutput

       ; And we are done !
       return(0)

    end
  5. Compile
  6. Test using a URL like http://localhost:8086/uniface/wrd/RESTBLOG?name=Theo
  7. Congratulations, you have just build your first Uniface RESTful web service!

NB: We did NOTHING in the Layout Editor!