Creating any kind of restful API with four HTTP methods only?
At the moment I'm trying to build a restful HTTP backend framework.
I've read a book called "Restful webservices" and it kicked off some brainwork on this area.
I have now a bigger picture about why resource oriented architecture is a good thing but there are still blurry parts I cannot understand. I'll try to explain my thoughts and see if someone could make me more clever.
Couldn't one say that everything is an object. Car, pen, book and even abstract things like an idea and a concept could be an object. Cause the word object is just a human invention for "something".
Couldn't you also say that every "something" is a resource. Coin, computer and even debt could be a resource. But the question is to whom. A debt is a resource, but not to the guy who owes, but to the guy he is owing. The same with human residues. They are resources, but not for us, but for mother nature because it needs balance - in and out - the basics of science (programming).
Resources (objects) seem to be nouns. How about adjective and verbs? It actually seems that everything could be described using nouns. Eg.
- Adjective: The car is red
- Noun: The car has a color red
- Adjective: I am tired
- Noun: I have a tiredness
- Verb: I kill him
- Noun: I create a kill
- Verb: I kiss her
- Noun: I create a kiss
This means that resource = object = noun. The same "something" from different perspectives.
Maybe there are verbs and adjectives that have no noun equivalent, but then that is only a flaw in the human language, not in the concept itself.
So back to what started all this.
When I really thought about that there are only 4 (I know there are some more) HTTP verbs - POST, GET, PUT, DELETE - I felt it couldn't create powerful restful APIs cause they are limiting the API to basic CRUD operations. But after some readings and thinking I realized that everything are just resources that could be either created, read, changed or deleted. Like in and out, simple rules, but yet powerful to create anything.
But then I thought, there is only "in" and "out". Maybe there is only "create" and "delete". Cause GET and PUT are verbs that could be replaced with "create a read" and "create a change".
All this is only me playing with the idea of basics of mother nature. In and Out, Create and Delete. The former is already widely accepted in the programming field. But the latter you don't hear about that much. But if that is correct, then this mean that HTTP Restful API could be used to create anything, in the right way, not by ha开发者_高级运维cking it with modified versions (putting the verbs in the uri, request body etc), but only using POST, GET, PUT, DELETE.
We just have to convert all methods to resources/objects. Instead of:
result = Books.search("Foo");
we have to think:
result = Search.create(Books, "Foo");
What do you think about this? With this in mind, could one create any kind of restful APIs with four HTTP methods only? Are "create" and "delete" another piece of the law of the nature?
I think you are relating two different aspects of a restful API. Reducing the HTTP methods to simply IN and OUT are already accomplished by request and response. Sure, you can map read to GET and PUT to create, but what about DELETE? Is that a "PUT of 0"? If so, then you require logic to handle that case.
For example, when you are opening a document into a text editor, you are performing an IN operation into the OS, and the OS performs and OUT operation to the text editor. The opposite is true for saving the document.
But that's just simple house keeping mechanics. Sure, the text editor can mask IN with GET and OUT with PUT, as in "save as", but what about DELETE? That would require it's own verb or overloading the PUT/OUT action to the OS. Then there's POST, which is equivalent to save*. Do we overload the PUT method to check to see if the file already exists? Why not just have it as its own verb?
If you're going to reduce to simple IN and OUT, then you have to overload OUT:
if(OUT){
if(file_exists) update_file
else if(file_size==0) delete_file
else create_file
}
*I'm speaking more theoretically, of course zzzzBov is correct in his post about HTTP's spec.
You can create any system using only two methods, GET and POST, by equating GET = Read and POST = Write. The other methods just help to add some visibility to the requests.
If you really want to try and model the REST request in terms of objects, I would do this:
result = new Search(Books,"Foo").Get();
However, I'm not sure this mapping is particularly valuable.
A RESTful API is essentially an interface to some kind of data store: a DB, a file system, a distributed hash table, &c. This means that you really don't need custom verbs (standard interfaces are usually better anyway) because you can get everything done using GET, PUT, POST, and DELETE.
It's also important to note that a RESTful API specifically calls for using existing HTTP methods to CRUD resources. Also, API's don't need to be complex or verbose to be useful or even powerful. In most cases simplicity is your friend. Simple structures and simple interfaces, in many cases, do a much better job than equivalent complex structures/interfaces. Look at git, for example, the data structures it uses are very, very, simple and git is very, very, fast as a consequence.
As for your question: yes, people do it all the time and it works!
But then I thought, there is only "in" and "out". Maybe there is only "create" and "delete". Cause GET and PUT are verbs that could be replaced with "create a read" and "create a change".
You could do this. You can go even further, and do everything with a POST
. You can then have an envelope inside your HTTP Request, that says the operation you wanted to perform. You could even have just one endpoint, and have as many different operations according to the content of your HTTP Request. You could have createBook
, updateBook
, getAllBooks
, and so on.
And you have SOAP
.
As someone who has had to build, maintain and code against SOAP and RESTful web services, do yourself (and everyone else) a favour, and use REST.
GET, POST, PUT, and DELETE do not directly relate to Create, Read, Update, and Delete. They often can, but it is important to note that POST and PUT can both perform Update and Create functionality.
http://en.wikipedia.org/wiki/POST_%28HTTP%29
the POST method should be used for any context in which a request is non-idempotent
This means that POST should be used for any function that changes server (data) state, and GET, PUT, and DELETE should be used for any functions that do not change server state.
EDIT:
To answer the question: yes. There are a number of solutions I've seen for creating a restful API with html headers. They all boil down to using a directory structure and the right HTML headers.
http://en.wikipedia.org/wiki/Representational_State_Transfer#RESTful_web_services
精彩评论