你如何实现资源“编辑"?以 RESTful 方式生成表单? [英] How do you implement resource "edit" forms in a RESTful way?

查看:49
本文介绍了你如何实现资源“编辑"?以 RESTful 方式生成表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在尝试为我们现在拥有的应用程序实现 REST API.我们希望使用 REST API 公开各种资源的读/写功能.我们如何实现其中的形式"部分?我了解如何通过创建本质上用作方法调用并返回数据的 RESTful URL 来公开读取"我们的数据:

We are trying to implement a REST API for an application we have now. We want to expose read/write capabilities for various resources using the REST API. How do we implement the "form" part of this? I get how to expose "read" of our data by creating RESTful URLs that essentially function as method calls and return the data:

GET /restapi/myobject?param=object-id-maybe

...并返回一个表示某种数据结构的 XML 文档.很好.

...and an XML document representing some data structure is returned. Fine.

但是,通常在 Web 应用程序中,编辑"将涉及两个请求:一个加载当前版本的资源并使用该数据填充表单,另一个将修改后的数据发布回来.

But, normally, in a web application, an "edit" would involve two requests: one to load the current version of the resources and populate the form with that data, and one to post the modified data back.

但我不明白你会如何使用 REST 映射到的 HTTP 方法做同样的事情.这是一个 PUT,对吧?有人能解释一下吗?

But I don't get how you would do the same thing with HTTP methods that REST is sort of mapped to. It's a PUT, right? Can someone explain this?

(额外考虑:UI 将主要使用 AJAX 完成)

(Additional consideration: The UI would be primarily done with AJAX)

--更新:这绝对有帮助.但是,我对服务器端还是有点困惑?显然,我不是在这里简单地处理文件.在服务器上,响应请求的代码应该过滤请求方法以确定如何处理它?那是读写之间的切换"吗?

-- Update: That definitely helps. But, I am still a bit confused about the server side? Obviously, I am not simply dealing with files here. On the server, the code that answers the requests should be filtering the request method to determine what to do with it? Is that the "switch" between reads and writes?

推荐答案

如果您通过纯 HTML 提交数据,您只能使用基于 POST 的表单.POST 请求发送到的 URI 不应是正在修改的资源的 URI.您应该 POST 到每次添加新创建资源的集合资源(在 Location 标头中包含新资源的 URI 和 202 状态代码)或 POST到更新程序资源,该资源使用请求内容(或自定义标头)中提供的 URI 更新资源.

If you're submitting the data via plain HTML, you're restricted to doing a POST based form. The URI that the POST request is sent to should not be the URI for the resource being modified. You should either POST to a collection resource that ADDs a newly created resource each time (with the URI for the new resource in the Location header and a 202 status code) or POST to an updater resource that updates a resource with a supplied URI in the request's content (or custom header).

如果您使用的是 XmlHttpRequest 对象,则可以将该方法设置为 PUT 并将数据提交到资源的 URI.如果服务器为尚不存在的资源提供有效的 URI,这也适用于空表单.第一个 PUT 将创建资源(返回 202).如果数据相同,则后续 PUT 将不执行任何操作,或者修改现有资源(在任何一种情况下,除非发生错误,否则将返回 200).

If you're using an XmlHttpRequest object, you can set the method to PUT and submit the data to the resource's URI. This can also work with empty forms if the server supplies a valid URI for the yet-nonexistent resource. The first PUT would create the resource (returning 202). Subsequent PUTs will either do nothing if it's the same data or modify the existing resource (in either case a 200 is returned unless an error occurs).

这篇关于你如何实现资源“编辑"?以 RESTful 方式生成表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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