Monday, September 30, 2024

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

Hello guys, if you are confused about whether to use PUT or POST for updating a resource using RESTful web service then you have come to the right place. In the past, I have shared the best RESTful web service books and courses and today, I am going to share some tips to choose between PUT and POST while designing your RESTful web services. Btw, you are not alone, one of the most common confusion among web developers is the choice of PUT or POST HTTP method for creating and 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 resources makes it easy, as you don't need to worry about the 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 the 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 resources 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 Master Java Web Services and RESTful API Course learn more about idempotent and safe methods of the HTTP protocol. One of the excellent resources for any web developer, 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 the 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:


Other RESTful Webservice articles you may like to explore
  • Top 10 RESTful web services interview questions for Java developers (see)
  • 5 Best Courses to learn RESTful Web Service using Spring (best courses)
  • Top 10 Java Web service interview questions (see here)
  • 10 Best Courses to learn Spring Framework (best courses)
  • REST Web Services framework Interview Question (see here)
  • 10 Free Spring Boot courses for beginners (free courses)
  • Difference between SOAP and RESTful web service in Java? (see here)
  • 10 Microservice and Spring courses in Java (online courses)
  • What are idempotent and safe methods of HTTP and REST? (answer)
  • Top 5 Courses to learn Spring MVC (online courses)
  • What is the purpose of different HTTP methods in REST? (answer)
  • 5 Books to prepare Java EE interviews (list)
  • Why Spring MVC is best to create REST API in Java (article)
  • 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.

1 comment:

Feel free to comment, ask questions if you have any doubt.