Presentation is loading. Please wait.

Presentation is loading. Please wait.

RESTful Web Services.

Similar presentations


Presentation on theme: "RESTful Web Services."— Presentation transcript:

1 RESTful Web Services

2 RESTful Web Services REST stands for REpresentational State Transfer.
REST revolves around resource where every component is a resource and a resource is accessed by a common interface using HTTP standard methods. REST Server simply provides access to resources and REST client accesses and modifies the resources. Each resource is identified by URIs/ global IDs. REST uses various representation to represent a resource like text, JSON, XML. JSON is the most popular one.

3 RESTful Web Services Web services based on REST Architecture are known as RESTful web services. RESTful web services use HTTP methods A RESTful web service usually defines a URI for a service and provides resource representation such as JSON.

4 Client–Server Architecture
Client application and server application MUST be able to evolve separately without any dependency on each other. A client should know only resource URIs. Servers and clients may be replaced and developed independently. Separating the user interface from the data storage. Improves the portability of the user interface across multiple platforms. It also improves scalability by simplifying the server components.

5 Uniform interface A resource in the system should have only one logical URI It’s always better to synonymies a resource with a web page. Any single resource should not be too large and contain each and everything in its representation. All resources should be accessible through a common approach such as HTTP GET. Once a developer becomes familiar with one of your API, he should be able to follow the similar approach for other APIs.

6 Stateless Server will not store anything about latest HTTP request client made. Server will treat each and every request as new. No session, no history. If client application needs to be a stateful, where user logs in once and do other authorized operations, then each request from the client should contain all the information necessary to service the request – including authentication and authorization details. The client is responsible for managing the state of the application.

7 Cacheable Caching of data and responses is important wherever they are applicable/possible. Caching brings performance improvement for client side, and better scope for scalability for a server because the load has reduced. In REST, caching shall be applied to resources when applicable and then these resources MUST declare themselves cacheable. Caching can be implemented on the server or client side. Well-managed caching partially or completely eliminates some client-server interactions.

8 Layered system REST allows you to use a layered system architecture where you deploy the APIs on server A, and store data on server B and authenticate requests in Server C, for example. A client cannot ordinarily tell whether it is connected directly to the end server, or to an intermediary along the way. Intermediary servers can improve system scalability by enabling load balancing and by providing shared caches.

9 Code on demand (optional)
Most of the time you send the static representations of resources in form of XML or JSON. But you may return executable code to support a part of your application. JavaScript, Java Applet Clients may call your API to get a UI widget rendering code. It is permitted.

10 RESTful uses HTTP Four HTTP methods are commonly used in REST.
GET − Provides a read only access to a resource. POST − Used to create a new resource. DELETE − Used to remove a resource. PUT − Used to update a existing resource or create a new resource.

11 REST API Frameworks ASP.Net (C#) Spring (Java) Express (Node.js)
Loopback (Node.js) Restify (Node.js) Django (Python) Flask (Python) Laravel (PHP) And many others…

12 Building REST APIs using Node.js (Express)
Express is integrated with minimal and flexible Node.js web application and has a robust set of features. npm install express --save

13 Building REST APIs using Node.js (Express)
var express = require('express'), app = express(); app.listen(3000); console.log('RESTful API server started on: ' ); var todoList = require('./todolistcontroler.js'); app.route('/') .get(todoList.home); app.route('/tasks') .get(todoList.list_all_tasks) .post(todoList.list_all_tasks);

14 Building REST APIs using Node.js (Express)
app.route('/tasks/r/:taskId') .get(todoList.list_all_tasks) .post(todoList.list_all_tasks); app.route('/tasks/a/:taskId/:taskName') .get(todoList.create_task) .put(todoList.update_task) app.route('/tasks/d/:taskId') .get(todoList.delete_task) .delete(todoList.delete_task);

15 todolistcontroler.js var taskdb=[];
exports.home = function(req, res) { res.sendFile(__dirname +'/todohome.html'); }; exports.list_all_tasks = function(req, res) { res.json(taskdb); exports.create_task = function(req, res) { taskdb.push({'id':req.params.taskId, 'name':req.params.taskName}); res.json("ok");

16 todolistcontroler.js exports.read_task = function(req, res) {
for (var i = 0; i < taskdb.length; i++) { if(taskdb[i].id==req.params.taskId){ res.json(taskdb[i]); return; } res.json("not found"); }; exports.update_task = function(req, res) { taskdb[i]=req.params.taskName; res.json("ok");

17 todolistcontroler.js exports.delete_task = function(req, res) {
for (var i = 0; i < taskdb.length; i++) { if(taskdb[i].id==req.params.taskId){ taskdb.splice(i, 1); res.json("ok"); return; } res.json("not found"); };

18 An HTML page to test REST APIs
<body> <h2>To Do Tasks</h2> <button type="button" onclick="list()">List</button> <button type="button" onclick="add()">Add/Upate</button> <button type="button" onclick="del()">Delete</button> <button type="button" onclick="del()">Read</button> <br/> <input type="text" id="id" value=""> <input type="text" id="name" value=""> <div id="result"></div>

19 An HTML page to test REST APIs
<script> function list() { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { tasks=JSON.parse(this.responseText); document.getElementById("result").innerHTML=""; for (var i = 0; i < tasks.length; i++) { document.getElementById("result").innerHTML+=tasks[i].id+" "+tasks[i].name; } }; xhttp.open("GET", "/tasks", true); xhttp.send();

20 An HTML page to test REST APIs
function add() { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById("result").innerHTML=this.responseText; } }; id=document.getElementById("id").value; name=document.getElementById("name").value; xhttp.open("GET", "/tasks/a/"+id+"/"+name, true); xhttp.send();

21 An HTML page to test REST APIs
function del() { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById("result").innerHTML=this.responseText; } }; id=document.getElementById("id").value; xhttp.open("GET", "/tasks/d/"+id, true); xhttp.send(); </script>


Download ppt "RESTful Web Services."

Similar presentations


Ads by Google