简体   繁体   English

我应该使用发布或放置来编辑数据库中的人员,又应该使用哪个添加新人员?

[英]Should I use Post or Put to edit a person in a database and which should I use to add a new person?

So here is what the method looks like now: 所以这是现在的方法:

POST person/personId/edit
https://api.example.com/*key*/person/*personId*/edit?FName=Blah

I want this to change the first name of person at personId to Blah. 我希望这可以将personId的人的名字更改为Blah。

And if I need to add a person I say: 如果我需要添加一个人,我会说:

PUT person/create
https://api.example.com/*key*/person/create

and it will add a person with a new personId. 它将添加一个具有新personId的人员。

My interpretation of POST and PUT has always been: 我对POST和PUT的解释一直是:

POST - The server will receive an entity which it can use to perform an operation or create a resource. POST-服务器将收到一个实体,该实体可用于执行操作或创建资源。 If the endpoint's intent is to create a resource, then POST will always create a NEW resource. 如果端点的意图是创建资源,则POST将始终创建一个NEW资源。 In any case, each POST will be treated without regards to the state of a resource. 在任何情况下,每个POST都将被处理而不考虑资源的状态。 You are simply posting information to the server for it to operate on. 您只是将信息发布到服务器上以便对其进行操作。

Example: 例:

  • Imagine a web service that will send a message to a user's cellphone. 想象一下将向用户的手机发送消息的Web服务。 A POST could be used to provide the necessary information to the server which may not be appropriate for a GET. POST可以用于向服务器提供可能不适用于GET的必要信息。 The request payload will include this information. 请求有效负载将包含此信息。 No resources are being created on the server, so the operation returns 200 OK, indicating that the operation was completed successfully. 没有在服务器上创建任何资源,因此该操作返回200 OK,表明该操作已成功完成。 The response may also include a body containing information from the server operation. 该响应还可以包括包含来自服务器操作的信息的主体。
  • Imagine a web service that creates a ticket that is posted on a bulletin board. 想象一下一个创建票证的Web服务,该票证发布在公告板上。 A POST could contain the information needed to make that post. POST可能包含发布该帖子所需的信息。 The information is persisted on the server and returns a 201 Created (and maybe a response body which contains a user id, or a more completed object resulting from the creation). 该信息将保留在服务器上,并返回201 Created(创建201)(可能还包含一个包含用户ID的响应正文,或者是由创建产生的更完整的对象)。 In all cases, when something is POSTed to this endpoint, a NEW ticket is created. 在所有情况下,将某些内容发布到此端点时,都会创建一个新票证。

PUT - The server will receive an entity (with an ID, for example) with the intent of creating or replacing a resource. PUT-服务器将收到一个旨在创建或替换资源的实体(例如,具有ID)。 If the resource already exists, it will be replaced with the one within the request, otherwise a new resource will be created. 如果资源已经存在,则将其替换为请求中的资源,否则将创建一个新资源。 In all cases, something is persisted on the server. 在所有情况下,某些东西都会保留在服务器上。 Some way to uniquely identify the entity must be provided. 必须提供一种唯一标识实体的方法。 In other words, the client is the one that creates the ID because it will be used if entity needs to be created. 换句话说,客户端是创建ID的客户端,因为如果需要创建实体,则将使用该ID。 Most people I know struggle with this reality. 我认识的大多数人都为这个现实而挣扎。

Example: 例:

  • A web service receives a payload form the client containing user information. Web服务从客户端接收包含用户信息的有效负载。 The expectation is that the user will be saved. 期望用户将被保存。 The server will check to see if that user exists. 服务器将检查该用户是否存在。 If it does, it will update that user by replacing it (in its entirety) with the new resource provided with the request and return 200 OK or 204 No Content. 如果是这样,它将通过请求提供的新资源替换用户(全部)来更新该用户,并返回200 OK或204 No Content。 If it does not exist, it will create it and return 201 Created. 如果不存在,它将创建它并返回201 Created。

The general convention is usually: 一般约定通常为:

GET    => READ
POST   => CREATE
DELETE => DELETE
PUT    => UPDATE

A difference I can see is that you are also using different URIs, what is most commonly use is a single resource URI. 我可以看到的区别是您还使用了不同的URI,最常用的是单个资源URI。 But, anyways that's debatable so it is a matter of how you like it. 但是,无论如何这值得商de,这取决于您的喜好。

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

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