在REST风格的HTML应用程序中放置表单/备选视图的位置? [英] Where to put forms / alternative views in a RESTful html app?

查看:136
本文介绍了在REST风格的HTML应用程序中放置表单/备选视图的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们假设一个Web应用程序,它为每个URI提供了一个很好的GET请求html视图,并允许通过POST / PUT / PATCH / WHATEVER更新底层资源。

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




编辑:为了澄清,我的问题不是关于提供JSON或什么的纯数据API,而是关于诸如Stackoverflow之类的HTML应用程序。例如,您可以在 / questions 和这个特定的 / questions / 24696982 这是有道理的。要让表单添加新问题,您必须使用 / questions / ask ,其中I我不确定是否可以。并且这种方式将POST形式发布到 / questions / ask / submit ,这似乎是错误的。向该URL发出GET请求会产生一个404(如果有的话它应该是405)。表单应该发布到 / questions 。我仍然想知道在RESTful系统中是否至少可以接受表单的URI。

网站一样,构建一个真正的RESTFull API的一种方式是分割前端和API - 我认为这是最好的方式(有些人可能不同意) - 也许其他人不这样想,但可以说前端团队得到了 www.domain 并且您的API团队获得了 api.domain

  GET api.domain / questions  - 检索票据列表
GET api.domain / questions / 12 - 检索特定票据
POST api。域/问题 - 创建新票
PUT api.domain / questions / 12 - 更新票#12
DELETE api.domain / questions / 12 - 删除票#12
PATCH api.domain / questions / 12 - 部分更新票#12#我只想显示这也存在 - 我真的不使用它...

真棒编辑:正如你可以看到的,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请求接受头信息
----------- -------------------------------------------------- ----------------------------
domain / questions获取应用程序/ json所有问题为json
domain / questions GET text / html将页面作为html提供所有问题
domain / questions / ask获取文本/ html您的html添加新问题
doma在/ questions POST应用程序/ json中添加一个新的问题(这将从./ask中调用以添加新问题
domain / questions / ask GET应用程序/ json 404状态码因为问题/请问您不要没有实施任何资源

示例-2:

  URL请求ACCEPT HEADER RESPONSE 
---------------------------- -------------------------------------------------- -----------
domain / questions / 12 GET应用程序/ json将ID 12的问题显示为JSON
domain / questions / 12 GET text / html显示HTML表示你的网页
domain / questions / 12 / edit你的html编辑问题
domain / questions / 12 PUT application / json更新ID 12的问题添加PATCH的东西..我不真的使用它,但如果你不喜欢t更新整个对象,你可以/应该使用PATCH而不是PUT:p
domain / questions / 12 / edit GET应用程序/ json 404状态码因为问题/问你没有实现任何资源

昨天我告诉你第一个想法(这是 - 我认为使用api作为一个团队对于前端和一个开发api的团队来说,这是一个更好的方式),但@jackweirdy评论说(感谢那个 - 我接着搜索了很多东西,并且正在看世界各地的开发者的其他播客以及他们如何做到这一点)一切取决于你 - 这是你的api,最终你/你的团队将决定一种方式。希望这可以帮助你或其他人寻找如何在REST背景上构建API。



编辑部分中的例子是(如果我说得对)不喜欢这里在stackoverflow


Let's assume an web application that for each URI presents a nice html view for GET requests and allows to update the underlying resource through POST/PUT/PATCH/WHATEVER.

How do I then expose various forms that actually allow performing such requests from the browser? And broader: assuming I have alternative views (possibly also HTML) for the same resource, where do I put those? Arguably, such forms can be considered alternative views, so having an answer to the broader question would be ideal.


Edit: To clarify, my question is not about pure data APIs serving JSON or whatnot, but about HTML apps such as Stackoverflow. For example you can get the collection of questions under /questions and this particular one at /questions/24696982 which makes sense. To get the form to add a new question, you will have to use /questions/ask, which I'm not sure is alright. And that form POSTs to /questions/ask/submit, which seems just plain wrong. Making a GET request to that URL yields a 404 (if anything it should be a 405). The form should be POSTing to /questions. Still I would like to know whether at least the URI for the form is considered acceptable in a RESTful system.

解决方案

You have a website like, the one way to build a real RESTFull API is to split the frontend and the API - thats in my opinion the best way (some may disagree) - maybe some other don't think like this but lets say the frontend team got www.domain and your team for the API got 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...   

AWESOME EDIT: As you can see also stackoverflow uses this method: api.stackexchange.com

So as you can see you can have these structure - but you also can have a form on www.domain/questions/ask and this form would send the request to api.domain/questions via POST. I want to refer to: https://thenewcircle.com/s/post/1221/designing_a_beautiful_rest_json_api_video its a really nice podcast you should have heard.

EDIT: (another point of view)

Another idea is that you can simply choose which content should come back (Json,XML,HTML) if your client sends you the right Accept-Header.

Example 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

Example-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

Yesterday I told you about the first idea (which is - I think for using an api as a team (one for frontend and one team that develops the api - a better way) but as @jackweirdy commented (thanks for that - i then searched a lot and was looking at other podcasts from developer around the world and how they would do that) below it's really all up to you - it's your api and at the end you/your team will decide for one way. Hope this helps you or other that looking for how to build a API on a REST background.

The examples in the EDIT-Section would be (if I got it right) not like here on stackoverflow

这篇关于在REST风格的HTML应用程序中放置表单/备选视图的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆