Web Services

Web Service Client

You will need to create a web service client when you want to access any third party web service. All you need is the system function #http which takes 3 parameters, URL, http-method and an optional body. The body may be required for http-method POST and may need to be enclosed in a JSON, XML or any other format as required by the API.

str host="http://maps.googleapis.com/maps/api/geocode/json?address=American+Embassy,+Dhaka,+Bangladesh"
str response=#http(host,"GET","")

The raw response from the host may be encoded in a JSON, XML or any other format as defined by the API. You will need to extract the relevant data from the raw response. Try the method ca_test_client.method for an example.

Web Service Server - RESTful with JSON

To publish a RESTful web service in DreamApps, you will need to define

a JSON query format - must have parameters method, sessionid and any number of other parameters

a JSON response format - must have parameters status and any number of other parameters

a method to generate the response from the query.

While naming the resources, please follow the following convention to avoid confusion.

Query format - name_q.json

Response format - name_r.json

Method - name.method

DreamApps doesn't allow any web service without authentication. So, all calls to DreamApps web services need a sessionid which is obtained by a call to authentication web service zx_auth which returns a sessionid on successful authentication.

Try the method ca_test_server.method for an example of using the web service ca_test, which is implemented by ca_test_q.json, ca_test_r.json and ca_test.method.

Creating, Extracting and Replacing Data in JSONs

There is a single system function #json to manipulate JSONs in DreamApps. It can take up to 3 parameters.

Creating a JSON needs just one parameter and can have two formats.

str s=#json(file "filename.json")

or,

str s=#json("valid-text-string")

Extracting a field-value from a JSON needs 2 parameters.

str s=#json(json-string-name, key)

Inserting a field-value into a JSON needs 3 parameters.

str s=#json(json-string-name, key, field-value)

See the method ca_test_server.method for examples of #json usage.