html - 在 RESTful html 应用程序中将表单/替代 View 放在哪里?

标签 html forms rest

让我们假设一个 Web 应用程序,它为每个 URI 呈现一个用于 GET 请求的漂亮 html View ,并允许通过 POST/PUT/PATCH/WHATEVER 更新底层资源。

然后我如何公开实际上允许从浏览器执行此类请求的各种表单?更广泛的是:假设我有相同资源的替代 View (可能还有 HTML),我应该把它们放在哪里?可以说,这些形式可以被视为替代观点,因此对更广泛的问题有一个答案是理想的。


编辑:澄清一下,我的问题不是关于服务于 JSON 或诸如此类的纯数据 API,而是关于 Stackoverflow 等 HTML 应用程序。比如你可以在/questions下得到题集这个特别的在/questions/24696982这是有道理的。要获取添加新问题的表单,您必须使用 /questions/ask ,我不确定是否可以。然后形成 POST 到 /questions/ask/submit ,这似乎完全错误。对该 URL 发出 GET 请求会产生 404(如果有的话,它应该是 405)。表单应该发布到 /questions。我仍然想知道至少表单的 URI 在 RESTful 系统中是否被认为是可接受的。

最佳答案

你有一个网站,构建一个真正的 RESTFull API 的一种方法是将前端和 API 分开——在我看来这是最好的方法(有些人可能不同意)——也许其他人不这么认为但是假设前端团队获得了 www.domain 而您的 API 团队获得了 api.domain

GET api.domain/questions - Retrieves a list of tickets   
GET api.domain/questions/12 - Retrieves a specific ticket   
POST api.domain/questions - Creates a new ticket  
PUT api.domain/questions/12 - Updates ticket #12  
DELETE api.domain/questions/12 - Deletes ticket #12  
PATCH api.domain/questions/12 - Partially updates ticket #12 #I only want to display that this also exists - i don't really use it...   

很棒的编辑:如您所见,stackoverflow 也使用此方法:api.stackexchange.com

如您所见,您可以拥有这些结构 - 但您也可以在 www.domain/questions/ask 上拥有一个表单,该表单会将请求发送到 api.domain/questions 通过 POST。我想引用:https://thenewcircle.com/s/post/1221/designing_a_beautiful_rest_json_api_video这是一个非常好的播客,您应该听过。

编辑:(另一种观点)

另一个想法是,如果您的客户向您发送正确的 Accept-Header,您可以简单地选择应该返回哪些内容(Json、XML、HTML)。

示例 1:

       URL           REQUEST       ACCEPT HEADER               RESPONSE                      
-----------------------------------------------------------------------------------------
domain/questions      GET        application/json   all questions as json
domain/questions      GET        text/html          the page as html with all questions
domain/questions/ask  GET        text/html          Your html for to add a new question
domain/questions      POST       application/json   Add a new new questions (this would be called from ./ask to add the new questions
domain/questions/ask  GET        application/json   404 Status-Code because on questions/ask you don't have implemented any resource

示例 2:

       URL              REQUEST     ACCEPT HEADER               RESPONSE                      
-----------------------------------------------------------------------------------------
domain/questions/12       GET      application/json   Shows the questions with the ID 12 as JSON
domain/questions/12       GET      text/html          Shows the HTML representation of your page
domain/questions/12/edit  GET      text/html          Your html for to edit a the question
domain/questions/12       PUT      application/json   Updates the questions with the ID 12 // just to add the PATCH thing.. i don't really use it but if you don't update the whole object you could/should use PATCH instead of PUT :p
domain/questions/12/edit  GET      application/json   404 Status-Code because on questions/ask you don't have implemented any resource

昨天我告诉你第一个想法(这是 - 我认为将 api 作为一个团队使用(一个用于前端,一个团队开发 api - 更好的方法)但正如@jackweirdy 评论的那样(谢谢 -然后我进行了很多搜索,并查看了来自世界各地开发人员的其他播客以及他们将如何做到这一点)下面这一切都完全取决于您 - 这是您的 api,最后您/您的团队将决定一种方式。希望这个帮助您或其他寻找如何在 REST 背景上构建 API 的人。

EDIT 部分中的示例(如果我做对了)与 stackoverflow 上的示例不同

关于html - 在 RESTful html 应用程序中将表单/替代 View 放在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24696982/

相关文章:

java - 处理多部分/表单数据输入

java - 无法在 Tomcat 7 上部署 Restful 简单代码。错误 java.lang.ClassNotFoundException : com. sun.jersey.spi.container.servlet.ServletContainer

javascript - 如何阻止 jQuery TourBus 在点击时创建两个实例?

html - 是否可以在选中的复选框标签之前设置样式::之前?

javascript - 水平和垂直滚动

javascript - 输入的动态价格金额

ruby-on-rails - 具有嵌套属性的复杂 Rails 表单 (Rails 5)

javascript - 如何绑定(bind)onclick事件来调用函数

javascript - AngularJS 对动态生成的输入指令的表单验证不适用于 ngForm

java - Hibernate实体虚拟列错误?