When to use PUT or POST in a 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 critical point to remember is that when you use PUT to create a resource, you need to provide the id  like shown in the following example:

PUT /book/{id}

Since most of the systems want to keep control of id, i.e. they want to generate id on 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 the POST method completes, it returns 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.

You can further see REST Java Web Services learn more about idempotent and safe methods of the HTTP protocol. One of the excellent resources for any web developers, including Java JEE programmers.

Difference between PUT and POST method in REST

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

And if you want a quick recap of what every HTTP methods do in REST web services here is a quick snapshot:

When to use PUT or POST in a RESTful Web Service?

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  book by Leonard Richardson, Sam Ruby, and David Heinemeier Hansson to learn more about REST web services and if you like online courses here are few courses you may want to check:

Further Learning
Java Web Services
REST API Design, Development & Management
RESTFul Services in Java using Jersey By Bryan Hansen

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)
  • 5 courses to learn Spring framework in depth (courses)
  • 10 REST API Books for Web Developers (books)
  • Top 5 Books to Learn REST and RESTful Web Services? (books)
  • 10 Spring MVC and REST Annotations for Java Developers
  • Top 5 Courses to learn RESTFul Web Services in Java (courses)

P.S. - If you are looking for online training to learn how to develop RESTful Web Services in Java using the Spring framework, I suggest you joining Eugen Paraschiv's REST with Spring course.

The course has various options depending upon your experience level and how much you want to learn like beginner's class, intermediate class, and master class. You can join the one which suits you better, though I suggest joining the Master Class if you are serious about becoming an expert Java REST developer.

No comments:

Post a Comment