使用POST和PUT http动词更新并插入? [英] Update and insert using POST and PUT http verb?

查看:127
本文介绍了使用POST和PUT http动词更新并插入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用POST HTTP Verb更新数据库会发生什么?

如果我使用PUT HTTP Verb更新数据库怎么办?

另外

如果使用POST HTTP Verb插入数据库会发生什么?

如果使用PUT HTTP动词插入数据库会发生什么?



我尝试了什么:



发布vs PUT以防更新和插入

What happen if I update database using POST HTTP Verb?
What happen if I update database using PUT HTTP Verb ?
Also
What happen if Insert into db using POST HTTP Verb ?
Also What happen if Insert into db using PUT HTTP Verb?

What I have tried:

Post vs PUT in case of Update and Insert

推荐答案

正如其他人已经清楚地提到的那样,这不是关于你的应用程序,而是关于协议。如果你是,

As others have already mentioned clearly, this is not about your app, rather about the protocol. If you are,
引用:

使用POST HTTP动词更新 ing 数据库?

updating database using POST HTTP Verb?

您违反了HTTP协议和这些动词的定义。您问题中的其他案例也是如此。在正确定义的HTTP API中,您将动词用作对资源(URL)的操作。阅读以下内容,

You are violating the HTTP protocol and the definitions for these verbs. Same is the case for other cases in your question. In a properly defined HTTP API, you utilize the verbs as actions on the resources, which are URLs. Read the following,

GET    /people
POST   /people/ request=body;stripped=intentionally
PUT    /people/5
DELETE /people/10

您能阅读上述端点的内容吗?如果您以 / people / 10 DELETE 发送请求作为动词,会发生什么?您是否可以在 GET 上实施只读政策,如果,如果您的代码在后台删除了吗?



它也不只是关于你,而是关于用户......关于你的用户如何使用API​​。就像您一样,您的用户也将阅读API并了解其功能,以及他们如何执行操作。



有几种工具可以让您的观众使用您的API,其中一些工具是智能的,了解行为是什么,可以让他们决定并生成测试工作流程。在这些情况下,正确的动词将帮助每个参与方 - 您,开发人员,工具本身。如果您对API的开发有不同的思路和方法,则允许您这样做,但这意味着您需要强制您的用户以类似的方式使用API​​。



关于Swagger规范|文档| Swagger | Swagger [ ^ ]

邮差| API开发环境 [ ^ ]



例如,我可以允许我的用户使用 DELETE 请求创建一个新帐户,我可以允许他们使用<更新他们的配置文件code> GET 请求。这对你来说听起来不错吗?

Can you read what the above endpoints will do? What would happen if you send a request to /people/10 with DELETE as verb? Can you enforce the policy of read-only on GET if, if, if your code does a deletion in the background?

It is also not just about you, it is about the users... It is about, how your users consume the API. Just like you, your users will also read the API and take a concept of what it does, and how they can perform an action.

There are several tools that let your audience consume your APIs, some of these tools are intelligent and understand what is the behavior and can let them decide and generate a test workflow. In these cases, a correct verb will help every party involved—you, the developer, tool itself. If you have a different thinking and approach for the development of APIs, you are allowed to, but that means you need to force your users to consume the API in the similar fashion.

About Swagger Specification | Documentation | Swagger | Swagger[^]
Postman | API Development Environment[^]

For example, I can allow my users to create a new account using a DELETE request, and I can allow them to update their profiles using GET request. Does that sound good to you?

Afzaal Ahmad Zeeshan:
Afzaal Ahmad Zeeshan:

正确的任务的正确动词是你需要在你的API中实现的!

Right verb for the right task is what you need to implement in your API!

忘记别人告诉你的自由 - 它是一个立面,它最终会伤害你。抱歉无耻归因。 :笑:



最后但并非最不重要的是,HTTP方法需要遵循标准,如幂等性,只读方法,安全方法等。请阅读此处了解更多信息那, HTTP方法的幂等性是什么? - Stack Overflow [ ^ ]。

Forget the freedom that others are telling you about—it's a facade and it will hurt you ultimately. Sorry for the shameless attribution. :laugh:

Last but not least, HTTP methods need to follow a standard, like idempotency, read-only methods, safe methods, etc. Read here for more on that, What is idempotency in HTTP methods? - Stack Overflow[^].


以下是关于这些的快速说明:



POST 和< b> PUT 只是提供给Web应用程序的HTML方法。您的应用程序对它们的作用以及它与数据库的交互方式将取决于其中的编程。



POST方法的经典REST定义是创建一种新的资源。在数据库中,这类似于INSERT。其中许多系统将返回创建资源的新URI。



PUT的定义是将资源放在指定的URI上。如果资源已经存在,这将等同于UPDATE语句;如果没有,则为INSERT语句。不需要返回URI,因为它在请求中指定。



所以在所有现实中,由API决定如何处理POST与PUT。



如需更深入地了解何时使用:

何时使用PUT或POST - RESTful cookbook [ ^ ]
Here is the quickie on these:

POST and PUT are just HTML methods that are fed into your web application. What your application does with them and how it interacts with the database is going to depend on the programming within it.

The classic REST definition of the POST method is to create a new resource. In a database this would be analogous to an INSERT. Many of these systems will return the new URI where the resource was created.

The definition for PUT is put the resource at a specified URI. If the resource already exists this would equate to an UPDATE statement; if not, an INSERT statement. No return is needed of the URI as it was specified in the request.

So in all reality, it is up to the API on how to handle POST vs PUT.

For a more in-depth view on when to use what:
When to use PUT or POST - The RESTful cookbook[^]


让google成为你的朋友;



REST - PUT vs POST - REST API教程 [ ^ ]



REST中的http-PUT与POST - 堆栈溢出 [ ^ ]
Let google be your friend;

REST – PUT vs POST – REST API Tutorial[^]

http - PUT vs. POST in REST - Stack Overflow[^]


这篇关于使用POST和PUT http动词更新并插入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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