Tuesday, 3 January 2012

POST vs PUT

This is just for my reference - which i read from articles

URI is noun and Methods are verbs
noun - refers some thing/verbs-actions

So PUT is used for CREATE/UPDATE a particular resource like
http://www.expensereports.com/sales/12345
in above example we either try to create a new sales report '12345' or updating it.

POST is again used for CREATE/UPDATE, but PUT might be a better option if we know what we are trying to update or create(hope we got it:)). Because POST is like handing over the request to a handler or manager who works on it.

So: To save an existing user, or one where the client generates the id and it's been verified that the id is unique:
PUT /user/12345 HTTP/1.1  <-- create the user providing the id 12345
Host: mydomain.com

GET /user/12345 HTTP/1.1  <-- return that user
Host: mydomain.com
Otherwise, use POST to initially create the object, and PUT to update the object:
POST /user HTTP/1.1   <--- create the user, server returns 12345
Host: mydomain.com

PUT /user/12345 HTTP/1.1  <--- update the user
Host: mydomain.com

No comments: