有关正确的REST api设计的问题,特别是在更新资源时的PUT操作 [英] Questions on proper REST api design specifically on the PUT action when updating a resource

查看:230
本文介绍了有关正确的REST api设计的问题,特别是在更新资源时的PUT操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个REST接口(不是我们所有人),我想更新一个资源。

I'm creating a REST interface (aren't we all), and I want to UPDATE a resource.

所以,我想要使用PUT。

So, I think to use a PUT.

所以,我读了这个

我带走的是我将PUT转换为这样的网址

My take away is that i PUT to a URL like this

/hc/api/v1/organizer/event/762d36c2-afc5-4c51-84eb-9b5b0ef2990c

带有有效负载,然后永久重定向到URL,它可以获取资源的更新版本。
在这种情况下,它恰好是相同的网址,不同的操作。

with a payload, then a permanent redirect to the URL that it can GET an updated version of the resource. In this case it happens to be the same URL, different action.

所以我的问题是:


  1. 我对使用PUT更新资源的理解是正确的,并且我对PUT使用的理解是正确的。

  2. 当客户获得时重定向是否对重定向的URL执行与原始URL相同的操作?如果它的依赖是大多数客户遵循的标准吗?

我问第二个问题,因为POSTMAN和我的JQuery AJAX调用是窒息。 JQuery因为net :: ERR_TOO_MANY_REDIRECTS。那么它是否会重新定向并再次尝试PUT,它会再次获得REDIRECT?

I ask the 2nd question, because POSTMAN and my JQuery AJAX calls are choking. JQuery because of net::ERR_TOO_MANY_REDIRECTS. So is it redirecting and trying the PUT again, which it will get another REDIRECT?

curl也会爆炸,但即使它说如果它获得301它会切换到GET,当我查看输出时,它似乎并没有这样做(下图)。

curl blows up too but even though it says if it gets a 301 it will switch to a GET, it doesn't really seem to do that when i look at the output (below).

当curl跟随重定向并且请求不是如果HTTP响应是301,302或303,它将使用GET执行以下请求(如POST或PUT)。如果响应代码是任何其他3xx代码,curl将重新发送以下请求使用相同的未经修改的方法。

CURL OUTPUT(为简洁而编辑)(还要注意它是如何表示将切换到GET [从POST不正确] ],但是它似乎无论如何都要做PUT):

CURL OUTPUT (edited for brevity) (also note how it says its going to switch to a GET [incorrectly from a POST], but then it seems to do a PUT anyway):

curl -X PUT -H "Authorization: Basic AUTHZ==" -H "Content-Type: application/json" -H "Cache-Control: no-cache" -H "Postman-Token: e80657f0-a8f5-af77-1d9d-d7bc22ed0b30" -d '{ JSONDATA"}' http://localhost:8080/hc/api/v1/organizer/event/762d36c2-afc5-4c51-84eb-9b5b0ef2990c -v -L
* Hostname was NOT found in DNS cache
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> PUT /hc/api/v1/organizer/event/762d36c2-afc5-4c51-84eb-9b5b0ef2990c HTTP/1.1
> User-Agent: curl/7.37.1
> Host: localhost:8080
> Accept: */*
> Authorization: Basic AUTHZ==
> Content-Type: application/json
> Cache-Control: no-cache
> Postman-Token: e80657f0-a8f5-af77-1d9d-d7bc22ed0b30
> Content-Length: 203
>
* upload completely sent off: 203 out of 203 bytes
< HTTP/1.1 301 Moved Permanently
< Connection: keep-alive
< X-Powered-By: Undertow/1
< Set-Cookie: rememberMe=deleteMe; Path=/hc; Max-Age=0; Expires=Fri, 20-Feb-2015 03:53:28 GMT
< Set-Cookie: JSESSIONID=uwI3_41LAa7vlvapTsrZdw10.macbook-air; path=/hc
* Server WildFly/8 is not blacklisted
< Server: WildFly/8
< Location: /hc/api/v1/organizer/event/762d36c2-afc5-4c51-84eb-9b5b0ef2990c
< Content-Length: 0
< Date: Sat, 21 Feb 2015 03:53:28 GMT
<
* Connection #0 to host localhost left intact
* Issue another request to this URL: 'http://localhost:8080/hc/api/v1/organizer/event/762d36c2-afc5-4c51-84eb-9b5b0ef2990c'
* Switch from POST to GET
* Found bundle for host localhost: 0x7f9e4b415430
* Re-using existing connection! (#0) with host localhost
* Connected to localhost (127.0.0.1) port 8080 (#0)
> PUT /hc/api/v1/organizer/event/762d36c2-afc5-4c51-84eb-9b5b0ef2990c HTTP/1.1
> User-Agent: curl/7.37.1
> Host: localhost:8080
> Accept: */*
> Authorization: Basic dGVzdHVzZXIxOlBhc3N3b3JkMQ==
> Content-Type: application/json
> Cache-Control: no-cache
> Postman-Token: e80657f0-a8f5-af77-1d9d-d7bc22ed0b30
>
< HTTP/1.1 500 Internal Server Error
< Connection: keep-alive
< Set-Cookie: JSESSIONID=fDXxlH2xI-0-DEaC6Dj5EhD9.macbook-air; path=/hc
< Content-Type: text/html; charset=UTF-8
< Content-Length: 8593
< Date: Sat, 21 Feb 2015 03:53:28 GMT
<

...故障随之而来...它实际上是一个PUT

...failure ensues... It actually does a PUT

提前感谢。

推荐答案

我认为你在301重定向部分读得太多了。

I think you're reading too much into the 301 redirect section.

如果您想使用PUT更新资源,请返回:

If you want to update a resource using PUT, return:


  • 201 :如果资源已创建

  • 200:使用更新的资源

有问题的301仅当实际存在重定向时才适用 - 例如,如果可以通过名称识别某些内容,并且您需要将其重定向到具有id或其他内容的URL。 (也许你重构并且人们仍在使用旧的端点)。

The 301 in question only applies if there actually is a redirect in question - like, if something can be identified by name, and you need to redirect it to a url that has the id or something. (Maybe you refactor and people are still consuming the old endpoint).

那么,你真的需要重定向你的PUT请求吗?因为您应该使用200在同一循环中发回更新的资源,如上所述,而不是重定向到GET。

So, do you really need to redirect your PUT requests? Because you should be sending back the updated resource within the same loop using 200, like stated above, instead of "redirecting to GET".

编辑:修复一些拼写。

这篇关于有关正确的REST api设计的问题,特别是在更新资源时的PUT操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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