When to use PUT or POST in RESTful Web Service

One of the most common confusion among web developers is the choice of PUT or POST HTTP method for creating or updating a resource while developing RESTful Web Services. Since both can be used to submit data, you can use either POST or PUT to create or update a resource. Many web developers want to use PUT for creating a resource on the server because it's idempotent. No matter, how many times you call the PUT, the state of the resource will not jeopardize. Since the possibility of re-submission is real on a slow network, using PUT to create resource makes it easy, as you don't need to worry about user clicking the submit button multiple times. But, the key point to remember is that when you use PUT to create a resource, you need to provide the id e.g.

PUT /book/{id}

Since most of the system want to keep control of id i.e. they want to generate id by their own in the database, rather than client specifying the id, PUT doesn't seem to be the right method to create a resource. It also poses the challenge of multiple clients creating a resource with the same id and instead of creating they end up updating the resource.


Even though POST is not idempotent, it is a better choice for creating resource because it doesn't require a user to provide the id of the resource. Instead, when POST method completes it return the id and URI of the newly created resource in the location header. Allowing the user to further update the resource because it has both id and URI of the resource. See HTTP: The Definitive Guide by David Gourley to learn more about idempotent and safe methods of the HTTP protocol. One of the must read books for any web developers, including Java JEE programmers.



Similarly, PUT is the better choice for updating resource because you already have an id for the resource and its idempotent, so even if the user submits multiple PUT request the state of the resource will not be compromised.


That's all about when to use PUT and POST HTTP methods in RESTful web service. The short answer is, use POST to create a resource and use PUT to update the resource. It's not mandatory, but this seems more logical unless you can foresee any issue which completely changes the opinion.  If you want to learn more about designing RESTful web service or want to truly understand the philosophy behind REST then see RESTful Web Services by Leonard Richardson, Sam Ruby, and David Heinemeier Hansson to learn more about

When to use PUT or POST in RESTful Web Service


Other RESTful WebService articles you may like to explore
  • Top 10 RESTful web services interview questions for Java developers (see)
  • Top 10 Java Web service interview questions (see here)
  • REST Web Services framework Interview Question (see here)
  • Difference between SOAP and RESTful web service in Java? (see here)
  • What are idempotent and safe methods of HTTP and REST? (answer)
  • What is the purpose of different HTTP methods in REST? (answer)
  • 5 Books to prepare Java EE interviews (list)

References
What are RESTful Web Services


No comments:

Post a Comment