如何RESTful发送HTML表格? [英] How to send HTML form RESTfully?

查看:85
本文介绍了如何RESTful发送HTML表格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个称为事实"的资源集合的URI,以及该集合中每个事实"资源的URI.

I have a URI for a collection of resources called 'facts', and URIs for each 'fact' resource in that collection.

我相信,应该使用GET请求创建新事实"的表单,但是我在确定应将其作为URI时遇到了麻烦.

The form for creating a new 'fact' should be requested with a GET, I believe, but I'm having trouble deciding what URI it should be made to.

对集合URI的GET应该返回事实"资源URI的列表.每个事实" URI应返回其内容作为对GET的响应.当然,实际的事实"创建将是POST(或PUT,具体取决于情况).

A GET to the collection URI should return a list of the 'fact' resource URIs. Each 'fact' URI should return its contents as a response to GET. The actual 'fact' creation would be a POST (or PUT, depending on the situation), of course.

我看到了一些选择,但似乎都不令人满意:

I see a few options, but none seem satisfactory:

  1. 添加事实" URI将引用的事实表单" URI.对此URI的GET给出HTML形式.仅仅为了描述资源而拥有另一个资源似乎是错误的.
  2. 对事实" URI进行的POST如果在标头中不包含任何表单数据,将返回该表单.然后,在用户填写表单后,它将对表单数据进行POST,并创建新的事实"资源.这似乎是一种更糟糕的方法.
  3. 不要通过网络发送表单,而是将其作为API的一部分包含在内.这似乎是RESTful的,因为REST API应该描述媒体类型,并且可以通过对事实"类型的描述来形成形式.这很奇怪.也许REST服务与常规网站是分开的,所以实际的HTML表单请求位于REST API之外的某个URI.
  4. 将HTML表单作为事实" URI响应的一部分.

为澄清起见,我正在尝试遵循Roy Fielding指定的真正的REST体系结构,而不是冒充REST的半熟RPC.

To clarify, I'm trying to follow true REST architecture as specified by Roy Fielding, not half-baked RPC posing as REST.

我开始认为#3即将出现.

edit: I'm starting to think #3 is on to something.

edit2:我认为一种解决方案是以CRUD方式进行常规的非REST HTML导航,然后前端适当地进行AJAX REST调用(或后端对其REST API进行内部调用).

edit2: I think a solution is to have regular non-REST HTML navigation in a CRUD manner, and then the frontend makes AJAX REST calls as appropriate (or the backend makes internal calls to its REST API).

我需要正确执行此服务的REST部分的原因是,我希望稍后允许其他非HTML客户端与其进行交互.

The reason I need to do the REST part of this service correctly is that I want to allow other non-HTML clients to interact with it later on.

推荐答案

在我看来,唯一干净的RESTful答案是1和3.

In my mind, the only cleanly RESTful answers are 1 and 3.

如我所见,对资源的描述是其自身的资源.问题是您是想通过应用程序的API来访问此资源,还是要使其成为API本身的一部分.

As I see it, the description of the resource is a resource of its own. The question is whether you want to make this resource accessible through your application's API or if you want to make it part of the API itself.

对于1,似乎RESTful使URI如下所示:

For 1, it seems RESTful make the URIs something like this:

获取/事实->所有事实 GET/facts/1->返回事实1(显然,id可能是单词或其他东西) GET/facts/create->返回适合于创建事实的表格 POST/事实->添加事实

GET /facts -> all facts GET /facts/1 -> returns fact 1 (obviously the id might be a word or something else) GET /facts/create -> returns a form appropriate for creating a fact POST /facts -> adds a fact

这篇关于如何RESTful发送HTML表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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