如何以REST方式发送HTML表单? [英] How to send HTML form RESTfully?

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

问题描述

我有一个名为facts"的资源集合的 URI,以及该集合中每个fact"资源的 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.对这个 URI 的 GET 给出了 HTML 表单.仅仅为了描述一个资源而拥有另一个资源似乎是错误的.
  2. 对 'facts' 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 -> 所有事实GET/facts/1 -> 返回事实 1(显然 id 可能是一个词或其他东西)GET/facts/create -> 返回适合创建事实的表单POST/facts -> 添加一个事实

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

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

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