By Phavanhna Douangboupha, 03/29/09
The three main basic ideas of a REST process are to process a client request, to response to the request, and return a result/data in XML format according to the request.
Two techniques that can be used are getting parameters from client through a URL query string and the use of HTTP methods. The first technique is easier to implement compared to the second technique. The main disadvantages of the first technique include the size of the URL string, the maximum length of the URL string consisting of query parameters, and a possible negative side effect. The first limitation, the size of URL string, can be overcome by using POST method instead of GET method for a client request. However, POST and GET methods should be applied according to a specific task (see the blog on Creating REST Web Service for more detail). Consequently, the HTTP methods are a better solution for REST service.
I have already talked about how to implement a REST client using a URL query string from my previous post, now I will talk about how I implement a REST service for the cross server client request using HTTP methods.
For this exercise, a database server, a web host server and a web client server are assigned in different machines and hosted by different domains. The database server is hosted at STREAMER. The web host server is hosted by CHW domain and finally the demonstrated web client request comes from GIBSON domain (Figure 1 shows system architect for the REST cross domain service). STREAMER is a house to MySQL database which is hosted in a different domain from the web server.
Figure 1: System architect for REST cross domain service
GIBSON is used as a demonstration client to request data from another domain (CHW). The four main files implemented here are a normal HTML file (index.html), a JavaScript file (client_local.js), and two php using cURL libraries files (getclient_chw.php and postclient_chw.php). The Javascript file is used to implement Ajax to send a request and to receive responded data sent back by a server asynchronously. The data is displayed on the HTML file. The JavaScript file uses HTTP request object to send a request to either getclient_chw.php (”How Many Users?” in the database) or postclient_chw.php (”Add New User” to the database). getclient_chw.php and postclient_chw.php use cURL libraries to set up a HTTP request where getclient_chw.php uses the GET method to request to get data from a server and postclient_chw.php uses the POST method to request to post data to a server (please see table 1 for the correspondent HTTP methods to the database query). Both of the files make a request to process data with the web server on CHW. The client side has no relation to the database server on STREAMER and it only sends a HTTP request which will later be checked by the web server.
Use Gibson to act as a client to make REST request (Figure 2) - http://people.rit.edu/~pxd8840/restclient/index.html . As you click one of the two buttons – “How Many Users?” (HTTP GET) and “Add New User” (HTTP POST), you will see that the displayed results are updated asynchronously. These are done via Ajax using REST service to perform a cross-domain request instead of the <script> tag hack solution as mentioned from my previous post.
Figure 2: index.html, client requests on GIBSON
On the web host server (CHW), there are two main files – rest_database.php and ConnectDB.php. ConnectDB.php is an Object Oriented PHP class that contains methods to connect to the database and the required methods to process or retrieve data from STREAMER database server. The rest_database.php file also contains all the logic check method that received from a client. The rest_database.php file is used to check what kind of request being made if it is a HTTP GET request or a HTTP POST request. According to the request, it retrieves data from the database. Then it creates XML responses to the client.
Table 1: HTTP method, REST action, and SQL command for a client request
| HTTP Request Method | REST Action | SQL database command | Description |
| GET | GET | SELECT | Search/Request for data (getdata) |
| POST | POST | INSERT | Add/Insert new data (postdata) |
List of files
Client Side
- index.html
- client_local.js
- postclient_chw.php
- getclient_chw.php
Server Side
- rest_database_php
- ConnectDB.php class code on the server side contains database connection and database process methods
References:
- http://proquest.safaribooksonline.com/0596101015/phpckbk-CHP-15-SECT-1#phpckbk-CHP-15-SECT-1
- http://www.checkupdown.com/status/E500.html
- http://www.icontact.com/forums/topic-264.html
Links to other blogs in this project
- Resource pooling and scheduling systems during a disaster and/or crisis situation using a handheld device and the World
Wide Web (WWW) technologies - Web-based resource tracking system implemented on a mobile device
- Location Tracking Techniques
- Creating REST Web Service
- REST Client simple Implementation and Test
- REST Service Implementation using HTTP – Cross Domain Request
Tags: Add new tag, client, location tracking, REST, web service, web-based resource tracking, webserver

