At the moment I'm trying to build a restful HTTP backend framework.
目前我正在尝试构建一个宁静的HTTP后端框架。
I've read a book called "Restful webservices" and it kicked off some brainwork on this area.
我读过一本名为“Restful webservices”的书,它在这个领域开始了一些脑力劳动。
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.
这意味着resource = object = noun。从不同的角度看同样的“东西”。
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.
当我真的想到只有4个(我知道还有更多)HTTP动词 - POST,GET,PUT,DELETE - 我觉得它无法创建强大的restful API,因为它们将API限制为基本的CRUD操作。但经过一些阅读和思考后,我意识到一切都只是可以创建,读取,更改或删除的资源。像进出一样,简单的规则,但创造任何东西,但功能强大。
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".
但后来我想,只有“在”和“在外”。也许只有“创造”和“删除”。原因GET和PUT是可以用“创建读取”和“创建更改”替换的动词。
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 hacking it with modified versions (putting the verbs in the uri, request body etc), but only using POST, GET, PUT, DELETE.
这一切只是我在玩大自然的基本知识。进出,创建和删除。前者已经在编程领域被广泛接受。但后者你没有听到那么多。但如果这是正确的,那么这意味着HTTP Restful API可以用于以正确的方式创建任何东西,而不是通过使用修改版本(将动词放在uri,请求体等)中来创建任何东西,但仅使用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?
你怎么看待这件事?考虑到这一点,是否可以使用四种HTTP方法创建任何类型的restful API? “创造”和“删除”另一部分自然法则吗?
5 个解决方案
#1
1
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.
我认为你关注的是一个宁静的API的两个不同方面。将HTTP方法简化为简单的IN和OUT已经通过请求和响应来完成。当然,你可以将read映射到GET和PUT来创建,但DELETE呢?那是“PUT为0”吗?如果是这样,那么你需要逻辑来处理这种情况。
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.
例如,当您将文档打开到文本编辑器中时,您正在对OS执行IN操作,并且OS对文本编辑器执行和OUT操作。保存文档的情况恰恰相反。
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?
但这只是简单的家务保持机制。当然,文本编辑器可以使用PUT屏蔽IN,使用PUT屏蔽OUT,如“另存为”,但是DELETE呢?这将需要它自己的动词或将PUT / OUT动作重载到OS。然后是POST,相当于save *。我们是否重载PUT方法以检查文件是否已存在?为什么不把它作为自己的动词呢?
If you're going to reduce to simple IN and OUT, then you have to overload OUT:
如果你要减少到简单的IN和OUT,那么你必须重载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.
*我在理论上说得更多,当然zzzzBov在关于HTTP规范的帖子中是正确的。
#2
1
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.
您只需使用两种方法(GET和POST)创建任何系统,等同于GET = Read和POST = Write。其他方法只是有助于为请求添加一些可见性。
If you really want to try and model the REST request in terms of objects, I would do this:
如果你真的想尝试根据对象建模REST请求,我会这样做:
result = new Search(Books,"Foo").Get();
However, I'm not sure this mapping is particularly valuable.
但是,我不确定这种映射是否特别有价值。
#3
1
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.
RESTful API本质上是某种数据存储的接口:DB,文件系统,分布式哈希表,&c。这意味着你真的不需要自定义动词(标准接口通常更好),因为你可以使用GET,PUT,POST和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.
同样重要的是要注意RESTful API专门要求使用现有的HTTP方法来处理CRUD资源。此外,API不需要复杂或冗长有用甚至功能强大。在大多数情况下,简单是你的朋友。在许多情况下,简单的结构和简单的接口比同等的复杂结构/接口做得好得多。例如,看看git,它使用的数据结构是非常非常简单的,因此git非常非常快。
As for your question: yes, people do it all the time and it works!
至于你的问题:是的,人们一直这样做而且它有效!
#4
1
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".
但后来我想,只有“在”和“在外”。也许只有“创造”和“删除”。原因GET和PUT是可以用“创建读取”和“创建更改”替换的动词。
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.
你可以做到这一点。你可以走得更远,用POST做一切。然后,您可以在HTTP请求中包含一个信封,该信封说明您要执行的操作。您甚至可以只有一个端点,并根据HTTP请求的内容拥有尽可能多的不同操作。您可以拥有createBook,updateBook,getAllBooks等。
And you have SOAP
.
你有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.
作为必须构建,维护和编写SOAP和RESTful Web服务代码的人,请自己(以及其他所有人)帮忙,并使用REST。
#5
0
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.
GET,POST,PUT和DELETE与创建,读取,更新和删除没有直接关系。它们通常可以,但重要的是要注意POST和PUT都可以执行更新和创建功能。
http://en.wikipedia.org/wiki/POST_%28HTTP%29
the POST method should be used for any context in which a request is non-idempotent
POST方法应该用于请求是非幂等的任何上下文
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.
这意味着POST应该用于任何改变服务器(数据)状态的函数,并且GET,PUT和DELETE应该用于任何不改变服务器状态的函数。
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.
编辑:回答这个问题:是的。我已经看到了许多使用html标头创建restful API的解决方案。它们都归结为使用目录结构和正确的HTML标头。
http://en.wikipedia.org/wiki/Representational_State_Transfer#RESTful_web_services
#1
1
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.
我认为你关注的是一个宁静的API的两个不同方面。将HTTP方法简化为简单的IN和OUT已经通过请求和响应来完成。当然,你可以将read映射到GET和PUT来创建,但DELETE呢?那是“PUT为0”吗?如果是这样,那么你需要逻辑来处理这种情况。
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.
例如,当您将文档打开到文本编辑器中时,您正在对OS执行IN操作,并且OS对文本编辑器执行和OUT操作。保存文档的情况恰恰相反。
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?
但这只是简单的家务保持机制。当然,文本编辑器可以使用PUT屏蔽IN,使用PUT屏蔽OUT,如“另存为”,但是DELETE呢?这将需要它自己的动词或将PUT / OUT动作重载到OS。然后是POST,相当于save *。我们是否重载PUT方法以检查文件是否已存在?为什么不把它作为自己的动词呢?
If you're going to reduce to simple IN and OUT, then you have to overload OUT:
如果你要减少到简单的IN和OUT,那么你必须重载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.
*我在理论上说得更多,当然zzzzBov在关于HTTP规范的帖子中是正确的。
#2
1
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.
您只需使用两种方法(GET和POST)创建任何系统,等同于GET = Read和POST = Write。其他方法只是有助于为请求添加一些可见性。
If you really want to try and model the REST request in terms of objects, I would do this:
如果你真的想尝试根据对象建模REST请求,我会这样做:
result = new Search(Books,"Foo").Get();
However, I'm not sure this mapping is particularly valuable.
但是,我不确定这种映射是否特别有价值。
#3
1
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.
RESTful API本质上是某种数据存储的接口:DB,文件系统,分布式哈希表,&c。这意味着你真的不需要自定义动词(标准接口通常更好),因为你可以使用GET,PUT,POST和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.
同样重要的是要注意RESTful API专门要求使用现有的HTTP方法来处理CRUD资源。此外,API不需要复杂或冗长有用甚至功能强大。在大多数情况下,简单是你的朋友。在许多情况下,简单的结构和简单的接口比同等的复杂结构/接口做得好得多。例如,看看git,它使用的数据结构是非常非常简单的,因此git非常非常快。
As for your question: yes, people do it all the time and it works!
至于你的问题:是的,人们一直这样做而且它有效!
#4
1
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".
但后来我想,只有“在”和“在外”。也许只有“创造”和“删除”。原因GET和PUT是可以用“创建读取”和“创建更改”替换的动词。
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.
你可以做到这一点。你可以走得更远,用POST做一切。然后,您可以在HTTP请求中包含一个信封,该信封说明您要执行的操作。您甚至可以只有一个端点,并根据HTTP请求的内容拥有尽可能多的不同操作。您可以拥有createBook,updateBook,getAllBooks等。
And you have SOAP
.
你有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.
作为必须构建,维护和编写SOAP和RESTful Web服务代码的人,请自己(以及其他所有人)帮忙,并使用REST。
#5
0
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.
GET,POST,PUT和DELETE与创建,读取,更新和删除没有直接关系。它们通常可以,但重要的是要注意POST和PUT都可以执行更新和创建功能。
http://en.wikipedia.org/wiki/POST_%28HTTP%29
the POST method should be used for any context in which a request is non-idempotent
POST方法应该用于请求是非幂等的任何上下文
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.
这意味着POST应该用于任何改变服务器(数据)状态的函数,并且GET,PUT和DELETE应该用于任何不改变服务器状态的函数。
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.
编辑:回答这个问题:是的。我已经看到了许多使用html标头创建restful API的解决方案。它们都归结为使用目录结构和正确的HTML标头。
http://en.wikipedia.org/wiki/Representational_State_Transfer#RESTful_web_services