简体   繁体   English

REST API upsert端点与创建和更新端点

[英]REST API upsert endpoint vs create and update endpoints

I'm writing a REST API that among the rest deals with creating/updating/deleting users. 我正在编写一个REST API,其余的用于创建/更新/删除用户。

What will be the best practice to write create/update users endpoints? 编写创建/更新用户端点的最佳实践是什么?

  1. One endpoint that deals with both create and update (=upsert) users POST requests. 一个端点同时处理创建和更新(= upsert)用户的POST请求。
  2. Two different endpoints - one that deals with create users POST requests and the other that deals with update users PATCH requests. 两个不同的端点-一个用于处理创建用户POST请求,另一个用于处理更新用户PATCH请求。

There's a lot of wrong assumptions here. 这里有很多错误的假设。 With REST, an endpoint represents a 'thing' not an 'action'. 使用REST,端点表示“事物”而不是“动作”。

Lets say that this thing is a user. 可以说这个东西是用户。 You might host a user under a uri such as: 您可以在uri下托管用户,例如:

http://example.org/users/roy

From here, all the actions become natural. 从这里开始,所有动作都变得自然。 Want to delete the user? 要删除用户吗?

DELETE /users/roy

Want to update it? 要更新吗?

PUT /users/roy

Want to retrieve it? 要检索吗?

GET /users/roy

Note that PUT is generally considered more RESTful than PATCH. 请注意,通常认为PUT比PATCH更RESTful。 It's a good idea to implement PUT if you are strictly going for REST. 如果您严格要求使用REST,则最好实施PUT。 I think the distinction and benefits of PUT vs PATCH are a bit off-topic for question. 我认为PUT与PATCH的区别和好处有点疑问。

Now you're left with one more action... how do you create new users? 现在您还有其他操作...如何创建新用户? Well, I would sum up the best practice as follows: 好吧,我将最佳实践总结如下:

If you can allow the client to determine the URI, you should probably use PUT. 如果可以允许客户端确定URI,则可能应该使用PUT。

PUT /users/roy - Should respond with a 201.

If you want to ensure that you really create a new user and not overwrite an old one, you can let the client use the If-None-Match: * header to force the server to reject the request if the resource already exists. 如果要确保确实创建一个新用户而不覆盖旧用户,则可以让客户端使用If-None-Match: *标头强制服务器拒绝请求(如果资源已经存在)。

I will say that the above is a best practice, but it's not the most common. 我会说以上是最佳实践,但这不是最常见的做法。 Commonly rest services are backed by some relational database, and instead of natural keys, integers are often used in URIs. 通常,REST服务由一些关系数据库支持,并且在URI中经常使用整数代替自然键。

This means that your url patterns look like 这意味着您的网址格式如下所示

/users/1234

It also means that the client can't know what the URI will be when creating a new resource. 这也意味着客户端在创建新资源时不知道URI是什么。 The typical workaround to this is to NOT use PUT for creating, but use some kind of collection resource and use POST to create a new user: 通常的解决方法是不使用PUT创建,而是使用某种收集资源并使用POST创建新用户:

POST /users/

A good API probably returns a Location header that has the uri to the newly created resource. 一个好的API可能会返回一个Location头,该头具有uri到新创建的资源。 A good client will know that whenever it receives a Location header after a non-safe method, it should clear its cache of that uri. 一个好的客户将知道,只要它在非安全方法之后收到Location标头,就应该清除该uri的缓存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM