在RESTful html应用程序中放置表单/替代视图的位置?

bac*_*dos 4 html forms rest

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

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


编辑:为了澄清,我的问题不是关于提供JSON或诸如此类的纯数据API,而是关于HTML应用程序,例如Stackoverflow.例如,您可以获得下面的问题集合,/questions以及/questions/24696982有意义的特定问题.要获得表单添加新问题,您将不得不使用/questions/ask,我不确定是否正常.那形成POST /questions/ask/submit,这似乎是完全错误的.向该URL发出GET请求会产生404(如果有的话,它应该是405).表单应该POST到/questions.我仍然想知道在RESTful系统中是否至少认为表单的URI是可接受的.

Dom*_*rer 5

你有一个网站,构建一个真正的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...   
Run Code Online (Sandbox Code Playgroud)

真棒编辑:正如您所看到的,stackoverflow也使用此方法:api.stackexchange.com

因此,您可以看到您可以拥有这些结构 - 但您也可以使用表单www.domain/questions/ask,此表单会将请求发送到api.domain/questionsvia 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
Run Code Online (Sandbox Code Playgroud)

实施例-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
Run Code Online (Sandbox Code Playgroud)

昨天我告诉了你关于第一个想法(这是 - 我认为使用api作为一个团队(一个用于前端和一个开发api的团队 - 一个更好的方法)但是@jackweirdy评论(感谢你 - 然后我搜索了很多,正在看世界各地开发商的其他播客,以及他们将如何做到这一点)下面真的由你决定 - 这是你的api,最后你/你的团队将决定一种方式.希望这有助于你或寻找如何在REST背景上构建API的其他方法.

EDIT-Section中的示例(如果我做对了)不像stackoverflow那样

  • 多米是对的;API != JSON 端点。你只是为这些叫做人类的奇怪机器人构建一个 API (2认同)

Sor*_*aru 0

在 100% RESTful Web 服务中,资源是使用描述性 URL 来标识的,即仅由名词短语组成的 URL。

一般来说,为了创建新资源,您将使用PUT,尽管某些框架(例如 Zend Framework 2,如果我没记错的话)用于POST此目的。因此,要创建问题,您可以PUT questions,然后在请求正文中提供问题标识符,或者PUT questions/{identifier},从而在 URL 中提供 id。