调用同时执行UPDATE和INSERT的API端点时是PUT还是POST HTTP动词? [英] PUT or POST HTTP verb when calling API endpoint which performs both UPDATE and INSERT?

查看:405
本文介绍了调用同时执行UPDATE和INSERT的API端点时是PUT还是POST HTTP动词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个RESTful Web API(用ASP .Net Core 2.1编写),该API从使用方的客户端应用程序接收更改日志".这是一个JSON类,其中包含在客户端应用程序以脱机模式工作时对数据库执行的所有修改.客户端应用程序上线后,它将向API发送自上次同步以来发生的所有更改,从而将其数据库与在线/实时数据库同步.因此,它向API发送了一个变更集/变更日志,其中包含用于各种表/对象的一堆UPDATE,INSERT和DELETE列表.

I have a RESTful Web API (written in ASP .Net Core 2.1) which receives a "change-log" from the consuming client app. This is a JSON class containing all the modifications to the database that were performed on the client app while it was working in offline mode. Once the client app goes online, it synchronizes its database with an online/live database by sending the API all the changes that have happened since the last sync. So it sends the API a change-set/change-log with a bunch of UPDATE, INSERT and DELETE lists for the various tables/objects.

在API方面,我实际上并没有从实时数据库中删除任何内容-我只是将内容标记为已删除(因此我将布尔字段设置为true,即deleted = true).因此,从技术上讲,该API仅对数据库执行INSERTS和UPDATES.

On the API side, I don't actually delete anything from the live database - I simply flag things as deleted (so I set a boolean field to true, i.e. deleted = true). So technically, the API only performs INSERTS and UPDATES on the database.

现在,在使用客户端应如何调用API的同步"端点方面,我有些矛盾.它应该将其称为POST还是PUT请求?由于API实际上同时执行UPDATE和INSERTS ...哪个HTTP动词更合适?甚至有关系吗?

Now I am at odds on how the consuming client should call this "synchronize" endpoint of the API. Should it call it as a POST or as a PUT request? Since the API is actually performing both UPDATES and INSERTS... Which HTTP verb is more appropriate? Does it even matter?

推荐答案

我可以告诉你我的经验.动静的动词含义清晰,不能被误解.但这并不能涵盖所有情况.

I can tell you my experience. The verb meanings in rest are clear and can't be misunderstood. But it doesn't cover all cases.

通常,我将PUT仅用于定义的实体更新.为了涵盖所有其他混合操作,我使用POST.

Usually I use PUT just for entity update, as defined. To cover all others hybrid operation I use POST.

因此,PUT API公平而干净,当您遇到POST时,最好多挖一点!

So the PUT api are fair and clean and when you encounter a POST, it's better you dig a little bit more!

这篇关于调用同时执行UPDATE和INSERT的API端点时是PUT还是POST HTTP动词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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