Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In REST is POST or PUT best suited for upsert operation?

Tags:

rest

post

put

I keep a key-value storage in the server for the client. If the user sends key "k1", then I upsert it to the database. Is this considered POST or PUT?

Also I have another operation that removes all existing keys and adds the new key. Is this POST or PUT because it clears records and adds a new one.

like image 611
Jimmy Avatar asked Aug 27 '13 16:08

Jimmy


People also ask

Should I use put or POST?

Use PUT when you want to modify a single resource which is already a part of resources collection. PUT overwrites the resource in its entirety. Use PATCH if request updates part of the resource. Use POST when you want to add a child resource under resources collection.

Can we use Put instead of POST in rest?

If you want to use POST, then you would do that to a list of questions. If you want to use PUT, then you would do that to a particular question. Great, both can be used, so which one should I use in my RESTful design: You do not need to support both PUT and POST.

Is it okay to use POST FOR REST API updates?

You may use the POST method to create, update and delete resources but this is considered a poor design. The different http verbs standardize modern ways of creating REST APIs.

What is difference between put and POST method in REST API?

Another important difference between the methods is that PUT is an idempotent method while POST is not. For instance, calling the PUT method multiple times will either create or update the same resource. On the contrary, multiple POST requests will lead to the creation of the same resource multiple times.


1 Answers

If the user send key "k1" then I upsert it to the database. Is this considered POST or PUT.

According to the HTTP specification:

The PUT method requests that the enclosed entity be stored under the supplied Request-URI. If the Request-URI refers to an already existing resource, the enclosed entity SHOULD be considered as a modified version of the one residing on the origin server. If the Request-URI does not point to an existing resource, and that URI is capable of being defined as a new resource by the requesting user agent, the origin server can create the resource with that URI.

I therefore think that the use of PUT for an insert or update is perfectly legitimate, provided that in both cases the URI is known in advance. If you're using the key as part of the URI (as k1 in http://www.somewhere.com/resources/k1) this should be the case. To be ideally RESTful, however, a GET to the same URL should also allow you to download the resource.

Also I have another operation which remove all existing keys and add the new key, is this POST or PUT because it clear records and add new one.

I don't think this operation could be considered RESTful because it does two things. It seems to be providing a macro to satisfy the needs of a particular client, rather than simple access to data. A standard RESTful design would be

  1. Getting a list of keys by sending a GET to the parent URL. In the example above, that would be http://www.somewhere.com/resources;
  2. Deleting each of those keys by sending a DELETE to http://www.somewhere.com/resources/k1;
  3. Adding the replacement by sending a PUT to http://www.somewhere.com/resources/k2.

It's less clear cut, but I think it would also be legitimate to delete all resources by sending a single DELETE request to http://www.somewhere.com/resources.

like image 83
Polly Shaw Avatar answered Sep 28 '22 03:09

Polly Shaw