如何编辑资源? [英] How to edit a resource?

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

问题描述

请原谅这个简单的问题,但似乎大多数搜索都试图告诉我哪些方法是为哪些操作而设计的.例如,创建 &编辑是 PUT,从复数(文章)创建是 POST,依此类推.(如果您不同意这一点,我只是将其用作示例.:)

Pardon the simple question, but it seems most searches try to tell me which methods are designed for what actions. Eg, create & edit is PUT, create from plural (articles) is POST, and so on. (If you disagree with this, i was just using it as an example. :)

话虽如此,您如何启动资源编辑?要创建资源,请使用已知 url 对不存在的 URL 执行 GET.例如,GET:mysite/resource_one.这然后返回一个表单,表单提交一个PUT到同一个地址和bam,资源就创建好了.

With that said, how do you initiate a resource edit? To create a resource, with a known url you preform a GET on an non-existing URL. Eg, GET:mysite/resource_one. This then returns a form, and the form submits a PUT to the same address and bam, the resource is created.

现在如何编辑相同的资源?使用为 CRUD 设计的 4 种方法,我遇到了问题,因为我似乎只能想到一种方法.转到资源.例如,GET:mysite/resource_one/edit.然后显示一个包含现有数据的表单,您对其进行编辑,然后将数据提交到 GET:mysite/resource_one.在一个似乎被设计为允许在不离开资源的情况下对资源执行完整 CRUD 的系统中,这对我来说似乎很奇怪.

Now how do you edit that same resource? With 4 Methods, designed for CRUD, i am having issues because i can only seem to think of one way. To go to a new resource. Eg, GET:mysite/resource_one/edit. This then presents a form with existing data, you edit it, and then the data is submitted to GET:mysite/resource_one. This seems odd to me in a system that seems to be designed to allow full CRUD to be preformed on a resource, without leaving said resource.

那么.. 正确的方法是什么?我的意思是,如果 GET:mysite/resource_one/edit 是正确的,那为什么不GET:mysite/resource_one/delete, GET:mysite/resource_two/create,等等..

So.. what is the proper method? I mean, if GET:mysite/resource_one/edit is right, then why not GET:mysite/resource_one/delete, GET:mysite/resource_two/create, and so on..

非常感谢回复!

推荐答案

URL 始终代表资源,而不是操作.因此,虽然 mysite/resource_one/edit 可能是启动编辑资源的页面的正确 URL,但它不是 REST API 本身的一部分,而是使用该 REST API 的网络应用程序的一部分操纵该资源.此外,在该示例中,mysite/resource_one 将是资源的一种令人困惑的表示.

The URL always represents the resource, not the action. Thus, while mysite/resource_one/edit might be a proper URL to a page that initiates editing a resource, it's not a part of the REST API itself, it's part of the web app that uses that REST API to manipulate that resource. Moreover, in that example mysite/resource_one would be a confusing representation of the resource.

要创建新资源,请在父资源上使用 POST,请求正文包含新资源的数据.响应包含新创建资源的 URL.

To create a new resource, you use POST on the parent resource, with the body of the request containing the data for the new resource. The response contains the URL for the newly created resource.

要更新现有资源,请在资源 URL 上使用 PUT,请求正文包含资源数据的完整或部分更新.

To update an existing resource, you use PUT on the resource URL, with the body of the request containing either full or partial update of the resource data.

这篇关于如何编辑资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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