Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 10-Mar-19 12:23pm
Comments
Bryian Tan 10-Mar-19 14:52pm    
Somebody will put you in REST jail. :) Personally, I don't see any issue, the verb is basically indicator to the API which method to use to serve the request.

As others have already mentioned clearly, this is not about your app, rather about the protocol. If you are,
Quote:
updating database using POST HTTP Verb?
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
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:
Right verb for the right task is what you need to implement in your API!
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[^].
 
Share this answer
 
v2
Comments
[no name] 11-Mar-19 4:41am    
So there is not side affect of using POST with updating resource ? If any plz explain..
Afzaal Ahmad Zeeshan 11-Mar-19 6:25am    
I have explained the side effects in the answer. Please reread 4th and 5th paragraph in my answer, again.
[no name] 11-Mar-19 4:43am    
If Both POST and PUT does the same things then why REST API Originator uses this two name.
Afzaal Ahmad Zeeshan 11-Mar-19 6:26am    
No, they do not do the same thing. It is a poorly application, that does the same thing in both their cases. I have made these mistakes in past and they have only been problematic to a larger audience.
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[^]
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900