With a quiet server, the same url (say / books / 1) can respond to many different verbs. These verbs, GET, POST, PUT and DELETE together with this path indicate what you want to do with the data on the server. The answer will answer your request.
REST is access to data in a predictable and reasonable way.
If you came from a strong PHP background, where each URL should map to a specific file, you're right, that doesn't make sense. The two most notable RESTful development environments, ASP.NET MVC and Rails, have special servers (or server logic) that read verbs and do special routing for you. This is what allows the "normal flow" of the application to go through as you expected. For PHP, there are frameworks that help with this, such as WSO2 WSF .
How REST works with web browsers
Take for example your example. We have messages and we want to delete them.
Let's start with a URL, for example / posts / 4. As you might expect, this shows post 4, its attributes, and some actions you could take. A request to provide this URL will look like GET /posts/4 . The response contains HTML that describes the element.
The user clicks the "Delete Item 4" link, part of the HTML. This sends a request like DELETE /posts/4 to the server. Note that he reused URL /posts/4 , but the logic should be different.
From HTML forms and web browsers, many of them will by default change the link with the method = "delete" to the link method = "post". You will need to use Javascript (something like this ) to change the verb. Ruby on Rails uses a hidden input field ( _method ) to indicate which method should be used on the form, as an alternative.
On the server side, the delete element logic is executed. He knows how to do this because of the verb in the request ( DELETE ), which corresponds to the action being performed. It is a key point of REST that HTTP verbs become meaningful.
After deleting an element, you can respond to a page, for example, "yep, done", or "no, sorry, you can’t do this," but it makes sense for the browser to put you somewhere else. An element deleted with a response to a redirect to GET /posts makes sense.
If you look at the server log, it will be very clear that everything was done with the server, but this is not as important as ...
How REST works with arbitrary data
Another key point of REST is that it works well with multiple data formats. Suppose you are writing a program that would like to read and interact with the blog programmatically. You may need all the messages provided in XML, instead of having to clear HTML for information.
GET /posts/4.xml intuitive: "Server, please give me xml describing message # 4." The answer will be that xml. The RESTful server makes it obvious how to get the right information.
When you made the DELETE /posts/4.xml request, you ask: "Server, please delete item # 4." An answer like “Good, of course” is usually enough to express what happened. Then the program can decide what else it wants and make another request.
source share