Understanding RESTful Services

Developers can use a variety of programming languages and approaches to implement web services. One of the most common approaches is to create a RESTful service. The REST in RESTful service stands for Representational State Transfer, which defines how an application must interact with the services. Developers can create RESTful services using any programming language. The following PHP script, Uppercase.php, converts the parameter it receives to uppercase characters:

The program reads as follows: Line 1: open angle bracket question mark p h p. Line 2: header open parenthesis double quote Access hyphen Control hyphen Allow hyphen Origin colon asterisk double quote close parenthesis semicolon. Line 3: header open parenthesis double quote Content hyphen Type colon application slash json semicolon charset equals U T F hyphen 8 double quote close parenthesis semicolon. Line 4: echo strtoupper open parenthesis dollar underscore GET open bracket double quote name double quote close bracket close parenthesis semicolon. Line 5: h t t p underscore response underscore code open parenthesis 200 close parenthesis semicolon. Line 6: question mark close angle bracket.

The script’s processing is straightforward. As you can see, the script uses the PHP $_GET function to retrieve the name passed to the script as a query-string parameter and then uses the strtoupper function to convert the name to uppercase. The script then returns the status value 200 to indicate success.

Developers can call RESTful services from most programming languages. The following Upper.HTML, uses JavaScript to call the RESTful service:

The program reads as follows: Line 1: open angle bracket exclamation DOCTYPE h t m l close angle bracket. Line 2: open angle bracket h t m l lang equals double quote en double quote close angle bracket. Line 3: open angle bracket head close angle bracket. Line 4: open angle bracket meta charset equals double quote u t f hyphen 8 double quote close angle bracket. Line 5: open angle bracket script close angle bracket. Line 6: function display Upper Name open parenthesis close parenthesis open brace. Line 7: var request equals new X M L H t t p Request open parenthesis close parenthesis semicolon. Line 8: request dot open open parenthesis double quote GET double quote comma double quote h t t p colon double slash w w w dot class hyphen files dot com slash Uppercase dot. Line 9: p h p question mark name equals Jamsa double quote close parenthesis semicolon. Line 10: request dot on ready state change equals function open parenthesis close parenthesis open brace. Line 11: double slash Check if the request is compete and was successful. Line 12: if open parenthesis this dot ready State equals equals 4 ampersand ampersand this dot status equals equals 200 close parenthesis open brace. Line 13: alert open parenthesis this dot response Text close parenthesis semicolon. Line 14: close brace. Line 15: close brace semicolon. Line 16: double slash Sending the request to the server. Line 17: request dot send open parenthesis close parenthesis semicolon. Line 18: close brace. Line 19: open angle bracket slash script close angle bracket. Line 20: open angle bracket slash head close angle bracket. Line 21: open angle bracket body onload equals double quote display Upper Name open parenthesis close parenthesis semicolon double quote close angle bracket. Line 22: open angle bracket slash body close angle bracket. Line 23: open angle bracket slash h t m l close angle bracket.

The JavaScript code uses AJAX (asynchronous JavaScript) to call the Uppercase.php restful API, passing to name to convert to uppercase as a query-string parameter. When the API completes, it “call back” the function specified returning the ready-state value 4 and the status value 200 to indicate success.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.240.75