rest put is for create or update

gudii9
gudii9 used Ask the Experts™
on
hi,

i was reading below that rest webservices put is for creating
https://www.tutorialspoint.com/restful/restful_introduction.htm

I remember reading some where POST is for create and PUT is to update.

can you please advise which HTTP method for which?

Sr.No.      HTTP Method      URI      Operation      Operation Type
1      GET      /UserService/users      Get list of users      Read Only
2      GET      /UserService/users/1      Get User with Id 1      Read Only
3      PUT      /UserService/users/2      Insert User with Id 2      Idempotent
4      POST      /UserService/users/2      Update User with Id 2      N/A
5      DELETE      /UserService/users/1      Delete User with Id 1      Idempotent
6      OPTIONS      /UserService/users      List the supported operations in web service      Read Only
why it says operation type for POST is N/A where as put is idempotent. what it means?

for working on rest examples why we need both below entries


<dependency>
                  <groupId>javax.ws.rs</groupId>
                  <artifactId>javax.ws.rs-api</artifactId>
                  <version>2.0.1</version>
            </dependency>
            <dependency>
                  <groupId>org.apache.cxf</groupId>
                  <artifactId>cxf-rt-rs-service-description</artifactId>
                  <version>3.0.0-milestone1</version>
            </dependency>


below is fine
<groupId>javax.ws.rs</groupId>
                  <artifactId>javax.ws.rs-api</artifactId>

but why we need cxf as below as well in pom.xml?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior Developer
Commented:
I remember reading some where POST is for create and PUT is to update.
REST uses HTTP: A POST request identifies the data handler by the URI, not the entitiy. A PUT request identifes the entity changed with the URI.

Richardson Maturity Model and
What is the difference between the HTTP methods POST and PUT?
Distinguished Expert 2018
Commented:
I remember reading some where POST is for create and PUT is to update.

This is a matter of debate and you will hear different opinions about this. Some people use POST for creation some other use PUT (I personally use PUT). Both are correct.
ste5anSenior Developer

Commented:
Well, gauntlet taken.

POST is to create an entity. Cause the POST URI identifies the data handler, not an entity.
PUT is to update an existing entity, cause the PUT URI identifies the entity.

Thus it's about HTTP semantics and proxy implementation. PUT is defined as idempotent, sending twice the same PUT request has no further effect. POST requests are not idempotent.
Idempotent: the result on the server is the same, independently of how often they are called.
So proxies may filter consecutive PUT requests.

So consider touching an entity three times. When you use a PUT request and your user sits behind a proxy, then this is an action which he can't accomplish.

Thus: POST and PUT are different.

A common pattern found which is ok are lists. It is acceptable to say "POST listUri,  change that object enclosed in the request" in opposite to "PUT objectUri change exactly that object, to the data enclosed in the request". But the difference is, that you don't know which object will be changed, cause this kind of decision is made by the list handler. In opposite to PUT. Here PUT is only allowed the changed the given objectUri. When there is a reason, that this is the wrong URI, PUT must return an error.
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Distinguished Expert 2018

Commented:
I didn't throw any glove so there's nothing to pick up. Please don't start me on idempotency, I know very well what idempotent is and the semantics behind POST and PUT. You said it yourself, PUT is idempotent and this (in my opinion) makes it a perfect candidate for creating a resource, since it also accounts for errors (if you accidentally send the very same request only one entity will be created).

So no argument here, this is a matter that has been debated extensively. You think POST is good for creating I think PUT is, both are correct, it's a matter of preferences.

Author

Commented:
Another difference between these two methods is that PUT is supposed to be idempotent, while POST is not. This means that if a PUT request is made N times, the result must be the same for all them. POST method does not have this guarantee.

why put request is made N times result must be same for all of them?

i am not clear on this how result will be same  after PUT operation?

Author

Commented:
A common pattern found which is ok are lists. It is acceptable to say "POST listUri,  change that object enclosed in the request" in opposite to "PUT objectUri change exactly that object, to the data enclosed in the request". But the difference is, that you don't know which object will be changed, cause this kind of decision is made by the list handler. In opposite to PUT. Here PUT is only allowed the changed the given objectUri. When there is a reason, that this is the wrong URI, PUT must return an error.

not clear difference between objectUri vs content

can you please elaborate or point to some relevant links, examples, resources?
Distinguished Expert 2018
Commented:
why put request is made N times result must be same for all of them?

i am not clear on this how result will be same  after PUT operation?

Imagine that you have a web site that users can open an account with. And now imagine a user that pushes the submit button many times. Would you want this user to be able to open many accounts on your web site or only one?

If you only want the user to open one account and any subsequent requests to be ignored, then your operation should be idempotent.
ste5anSenior Developer
Commented:
why put request is made N times result must be same for all of them?
 i am not clear on this how result will be same  after PUT operation?
This behavior is mandatory per definition for PUT.

PUT is declared to be idempotent. Idempotent means, that the server state after each identical PUT request is the same. As a consequence, the result of each identical PUT request must be the same.
Thus any involved participant in between the client and the destination web server uses this definition. For example web proxies between the client and the web server will now only send the first PUT request of a sequence of identical PUT request to reduce load.

PUT is to update an existing entity, cause the PUT URI identifies the entity.
This is just a general rule.
As an concrete example: The server state (database) don't change, whether is set e.g. Color=Red once or more times. The color is always red afterwards.

Using girionis example: When you call a second "Create Account 123", then the operation must fail, cause you have created it earlier. Here PUT is acceptable for creating the account. Cause you can only create on account 123.

A different example are orders: "Create new Order for Customer 321". This can obviously be called multiple times to create multiple orders. Thus the create call must be POST. Otherwise proxies may no allow it due to the definition of PUT's behavior.

http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
ste5anSenior Developer

Commented:
not clear difference between objectUri vs content
It's POST listUri vs. PUT objectUri.

The listUri identifies an handler, not an object or entity. The handler can modify any entity (as definied by its requirements or logic). You cannot derive which entity or object is involved from that listUri.

The objectUri identifies an object or entity. The handler called must work on this object to be deterministic.

Content denotes the message body of a HTTP request, which is placed in the body as XML, JSON or even plain text or what ever format your service supports.

http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html
Distinguished Expert 2018

Commented:
This is a very delicate subject, since people are "divided" into two camps: those who favour PUT vs those who favour POST, for creating resources. I believe points should be equally divided as well.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial