[英]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: 例:
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: 例:
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.