Friday, June 3, 2011

What is REST?

http://blogs.windwardreports.com/tomasr/2011/04/what-is-rest.html


Let me give you a very simple, practical explanation.
  • REST is a methodology for creating web services.
  • The point of RESTful web services is to be as simple as possible.
  • RESTful web services use HTTP Methods: GET, POST, DELETE, PUT. You do not use the URL or the request body to specify the method.
  • RESTful web services use URLs specify what objects should be operated upon.
  • RESTFUL web services use HTTP status codes as return values.
  • The HTTP request body of a RESTful web service call is used only for data - not to specify the method, target objects, or return value.
The point of writing web services using REST is to take advantage of the simplicity of the HTTP protocol, and not to work against it. Your web service calls end up being very simple and easy to understand.
Here's an example of using a REST web service. This is a javascript function that retrieves a task object from the web service.
function sendGetTaskRequest(packet, template, task, handler) {
// This line creates a web request
requestGetTask = new XMLHttpRequest();

// This line specifies the HTTP method and the URL.
// Notice the URL is simply a path to an object.
requestGetTask.open("GET", baseUrl + "/v1/packets/" + packet + 
"/templates/" + template + "/tasks/" + task);

// This line hooks up a method to handle the response
requestGetTask.onreadystatechange = handler;

// This line sends the request
requestGetTask.send();
}

So to get a task, I just have to make a GET request to the task URL. To add a task, I could just POST to the tasks URL. REST makes web services very easy to use.
If you want a really good book to learn about REST, I recommend RESTful Web Services by Leonard Richardson and Sam Ruby.

http://blog.mwaysolutions.com/2014/06/05/10-best-practices-for-better-restful-api/
http://www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api
http://www.restapitutorial.com/resources.html

No comments:

Post a Comment