Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between REST call and URL

I have been into web development from sometime. But recently came across an old technology, REST. I read various places about REST calls, what I have understood about REST service is,

  • REST service responds back with JSON or XML data, which can be used on client side for rendering the DOM elements.

  • It enhances the use of HTTP protocol.

  • The URL difference between a REST call and normal URL is:

    REST CALL: wwww.xyz.com/getCart/12

    URL: wwww.xyz.com/getCart.php?cartId=12

I got the basic difference, hitting the URL would render a page at the server end and would return the response, whereas making an AJAX Call to the REST service would simply return a JSON or a XML output which can be parsed at the client end.

My question is:

If I make my .php page to render a JSON string, and the application makes a AJAX call to the php page to get the JSON response back and use it on client side to render the DOM, then what is the difference between REST call and a normal URL call.?

How REST calls are configured differently from normal URLs?

like image 714
Veer Shrivastav Avatar asked Jun 03 '14 12:06

Veer Shrivastav


1 Answers

There's a lot of misinformation and confusion about REST. I'm not surprised that these three points are what you understood from the information available, but they are wrong.

  • REST isn't coupled to any particular data format or media type. The most important constraint in REST is the emphasis on an uniform interface, which means in this case that the server should be able to respond with whatever data format or media type the clients accept. Under HTTP, the client will tell what formats it can understand through the Accept header, and the server should comply or fail with a 406 Not Acceptable error.

  • In the same way, REST isn't coupled to any particular protocol, although it's often convoluted with HTTP. Again, following the uniform interface, the clients should be able to follow any links provided by the server, for any protocol with a valid URI scheme.

  • The semantics of URLs are completely irrelevant to REST. All that matters to REST is that an URL identifies one and only one resource. The URL is an atomic identifier and the client shouldn't rely on any semantics embedded in it for any operations. The two examples you give are both valid in REST. There's nothing more or less RESTful about any of them.

To answer your question, under a REST application the difference you imagine doesn't exist. Hitting an URL will return a response. If the client is requesting with an Accept: text/html header, it may return the human-friendly html page to be rendered by a browser. If the client requests with an Accept: application/json or Accept: application/xml, it may return a machine-friendly format to be read by another application.

like image 52
Pedro Werneck Avatar answered Oct 17 '22 13:10

Pedro Werneck