英文:
How do these two patterns differ, with regards to API design?
问题
"我正在学习API设计,根据我正在阅读的博客,GET http://www.nowhere.com/images/getImage?imageId=123
是一个反模式,应该避免使用。虽然博客没有提到这一点,我猜想首选方法应该是 GET http://www.nowhere.com/images/123
。为什么第二种方法是首选方法?第一种方法有什么问题/限制?"
英文:
I am learning API design, and based on the blog I am reading
GET http://www.nowhere.com/images/getImage?imageId=123
is an antipattern, and should be avoided.
while the blog does not mention this, I guess the preferred method would be
GET http://www.nowhere.com/images/123
How is the second method the preferred method? what exactly is wrong/limitation with the first method?
答案1
得分: 1
第二种方法为何是首选方法?第一种方法有什么确切的问题/限制?
在REST的背景下,两种拼写都可以;REST不关心你在资源标识符中使用的拼写约定。
第一种变体在与HTML表单集成时更为方便。第二种变体在想要使用相对引用时更为方便。
设计师对第一种拼写存在偏见,因为它似乎反映了对HTTP中资源模型工作方式的误解。
另一种表达方式是:资源标识符是您正在获取的文档的标识符,您可以将其视为文档的名称。
因此,争论的焦点是"getImage"(一个动词短语)是否适合作为文档(名词)的良好名称,就像您在代码审查中提议将"getImage"作为持有对数据结构引用的变量名时可能会遇到反对一样。
与代码中的变量名一样,机器都不在乎,两种拼写都可以很好地工作。但我们更频繁地在浏览器位置控件、浏览历史、书签列表、访问日志等中看到资源标识符,所以它们更容易让人类使用是更为重要的。
英文:
> How is the second method the preferred method? what exactly is wrong/limitation with the first method?
Both spellings, as far as REST is concerned, are fine; REST doesn't care what spelling conventions you use for your resource identifiers.
The first variation is more convenient when you are trying to integrate with HTML forms. The second variation is more convenient when you want to use relative references.
There is a bias among designers against the first spelling, because it has the appearance of reflecting a misunderstanding of how resource models work in HTTP.
GET /images/123
Is closely analogous to this psuedo code
resouce("/images/123").get()
Similarly
GET /images/getImage?imageId=123
is analogous to
resource("/images/getImage?imageId=123").get()
Contrast this with:
resource("/images").getImage(imageId=123)
Expressed another way: the resource identifier is the identifier of the document that you are fetching, which you might think of as the name of the document.
So the argument is about whether "getImage" (a verb phrase) makes a good name for a document (noun), in much the same way that you might get push back in a code review were you to propose "getImage" as a variable name that holds a reference to a data structure.
Much like variable names in code, the machines don't care, either spelling will work just fine. But we see resource identifiers (in browser location controls, browser histories, bookmark lists, access logs, etc) a lot more frequently than we do variable names, so it is more important that they are easy for humans to work with.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论