REST API - HTTP Methods

In this article, we will learn the frequently used HTTP methods in building RESTful APIs.

REST APIs enable you to develop any kind of web application having all possible CRUD (create, retrieve, update, delete) operations. REST guidelines suggest using a specific HTTP method on a particular type of call made to the server.

Learn about RESTful APIs at https://www.javaguides.net/p/restful-tutorial.html

Let's learn about HTTP methods and their role in client-server communication over HTTP.

HTTP GET Method

Use GET requests to retrieve resource representation/information only – and not to modify it in any way. As GET requests do not change the state of the resource, these are said to be safe methods. Additionally, GET APIs should be idempotent, which means that making multiple identical requests must produce the same result every time until another API (POST or PUT) has changed the state of the resource on the server.

If the address is successfully received (error-free), GET returns a JSON or XML representation in combination with the HTTP status code 200 (OK). In case of an error, the code 404 (NOT FOUND) or 400 (BAD REQUEST) is usually returned.

Examples for GET request URI's:

HTTP GET - http://www.usermanagement/api/users/me - Get logged in user profile

HTTP GET - http://www.usermanagement/api/users/{username}/profile - Get user profile by username

HTTP GET - http://www.usermanagement/api/users/{username}/posts - Get posts created by user

HTTP GET - http://www.usermanagement/api/users/{username}/albums - Get albums created by user

http://www.usermanagement/api/users/checkUsernameAvailability - Check if username is available to register

http://www.usermanagement/api/users/checkEmailAvailability - Check if email is available to register

HTTP POST Method

The HTTP POST request is most commonly used to create new resources. When talking strictly in terms of REST, POST methods are used to create a new resource into the collection of resources.

Upon successful creation, an HTTP code 201 is returned, and the address of the created resource is also transmitted in the ‘Location’ header. 

Here are some examples for HTTP POST requests:

HTTP POST http://www.domain/api/users - Create User

HTTP POST http://www.domain/api/posts - Create Post

HTTP POST http://www.domain/api/posts/{postId}/comments - Create new comment for post with id = postId

HTTP PUT Method

Use PUT APIs primarily to update existing resource (if the resource does not exist, then API may decide to create a new resource or not). If a new resource has been created by the PUT API, the origin server MUST inform the user agent via the HTTP response code 201 (Created) response and if an existing resource is modified, either the 200 (OK) or 204 (No Content) response codes SHOULD be sent to indicate successful completion of the request.

Here are some examples for HTTP Put requests:

HTTP PUT http://www.domain/api/users/{username} - Update user

HTTP PUT http://www.domain/api/posts/{id} - Update post by id

HTTP PUT http://www.domain/api/posts/{postId}/comments/{id} - Update comment by id if it belongs to post with id = postId

The difference between the POST and PUT APIs can be observed in request URIs. POST requests are made on resource collections, whereas PUT requests are made on a single resource.

HTTP DELETE Method

Use DELETE APIs to delete resources (identified by the Request-URI).

A successful response of DELETE requests SHOULD be HTTP response code 200 (OK) if the response includes an entity describing the status, 202 (Accepted) if the action has been queued, or 204 (No Content) if the action has been performed but the response does not include an entity.

Here are some examples for HTTP Delete requests:

DELETE http://www.domain/api/users/{username} - Delete user

DELETE http://www.domain/api/posts/{id} - Delete post 

DELETE http://www.domain/api/posts/{postId}/comments/{id} - Delete comment by id if it belongs to post with id = postId

HTTP PATCH Method

HTTP PATCH requests are to make partial updates on a resource. If you see PUT requests also modify a resource entity, so to make more clear – the PATCH method is the correct choice for partially updating an existing resource, and PUT should only be used if you’re replacing a resource in its entirety.

Example:

A sample patch request to update the email will be like this:

HTTP PATCH /users/1

[

{ “op”: “replace”, “path”: “/email”, “value”: “new.email@example.org” }

]

When to Use HTTP PUT and When HTTP PATCH?

When a client needs to replace an existing Resource entirely, they can use PUT. When they're doing a partial update, they can use HTTP PATCH.

For instance, when updating a single field of the Resource, sending the complete Resource representation might be cumbersome and utilizes a lot of unnecessary bandwidth. In such cases, the semantics of PATCH make a lot more sense.

Another important aspect to consider here is idempotence; PUT is idempotent; PATCH can be, but isn't required to. And, so – depending on the semantics of the operation we're implementing, we can also choose one or the other based on this characteristic.

Summary of HTTP Methods

REST API's for User Management Application - Following five REST APIs for User resource:

Conclusion

In this article, we have learned about commonly used HTTP methods in RESTful API with examples.

Learn how to develop REST API's using the Jersey framework at https://www.javaguides.net/p/restful-tutorial.html

Learn how to develop REST API's using JAX RS with RESTEasy framework at https://www.javaguides.net/2020/01/resteasy-crud-example-tutorial.html

Related REST Articles

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours


Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course

Comments

  1. I am sure it will help many people. Keep up the good work. It's very compelling and I enjoyed browsing the entire blog.
    Business Analytics Course in Bangalore

    ReplyDelete
  2. Really, this article is truly one of the best in the article. And this one that I found quite fascinating and should be part of my collection. Very good work!.
    Data Science Training in Jaipur

    ReplyDelete
  3. Very informative blog! There is so much information here that can help thank you for sharing.
    Data Science Syllabus

    ReplyDelete
  4. Really impressed! Information shared was very helpful Your website is very valuable. Thanks for sharing..
    Business Analytics Course in Bangalore

    ReplyDelete
  5. I have read your article, it is very informative and useful to me, I admire the valuable information you offer in your articles. Thanks for posting it ...

    Business Analytics Course in Patna

    ReplyDelete
  6. This is definitely one of my favorite blogs. Every post published did impress me.
    Mlops Training

    ReplyDelete
  7. This is an informative and knowledgeable article. therefore, I would like to thank you for your effort in writing this article.
    Data Scientist Course in Lucknow

    ReplyDelete
  8. Very informative message! There is so much information here that can help me thank you for sharing
    Data Analytics Course in Lucknow

    ReplyDelete
  9. Really nice and amazing post. I was looking for this kind of information, Keep posting. Thanks for sharing.
    Data Science Courses in Bangalore

    ReplyDelete
  10. Very informative Blog! There is so much information here that can help thank you for sharing.
    Data Analytics Training in Bangalore

    ReplyDelete
  11. I enjoyed reading your articles. I have bookmarked it and I am looking forward to reading new articles. Thanks for sharing.
    Business Analytics Course in Indore

    ReplyDelete
  12. Excellently written article and information was helpful. Please keep it up thank for sharing.
    Business Analytics Course in Lucknow

    ReplyDelete
  13. We are really grateful for your blog post. You will find a lot of approaches after visiting your post. Great work thank you.
    Data Analytics Course in Chandigarh

    ReplyDelete
  14. Very useful article to read and Information was helpful.I would like to thank you for the efforts you had made for writing this awesome article.
    Data Analytics Course

    ReplyDelete

  15. Very nice job... Thanks for sharing this amazing and educative blog post!
    Data Science Training in Chandigarh

    ReplyDelete
  16. Very good message. I stumbled across your blog and wanted to say that I really enjoyed reading your articles. Anyway, I will subscribe to your feed and hope you post again soon.
    Data Scientist Course in India

    ReplyDelete
  17. I couldn't leave your website until I told you that I really appreciated the high quality information it presents to your visitors. I will come back frequently to check for new posts.

    Data Scientist Course in Durgapur

    ReplyDelete
  18. A good blog always contains new and exciting information and as I read it I felt that this blog really has all of these qualities that make a blog.
    Data Science Institutes in Bangalore

    ReplyDelete
  19. I got to this amazing site not long ago. I actually captured with the piece of resources you have got here. Thanks for making such wonderful blog page.
    Business Analytics Course in Indore

    ReplyDelete
  20. This is an informative and knowledgeable article. therefore, I would like to thank you for your effort in writing this article.
    Data Science Course in Bangalore

    ReplyDelete
  21. The information you have posted is very useful and knowledgeable. Thanks for sharing.
    Data Analytics Course in Lucknow

    ReplyDelete
  22. I wanted to leave a little comment to support you and wish you the best of luck. We wish you the best of luck in all of your blogging endeavors.

    Data Analytics Course in Patna

    ReplyDelete
  23. Hi, This article is probably where I got the most useful information for my research. Do you know of any other websites on this topic?
    Data Science Training in Ahmedabad

    ReplyDelete
  24. Some really good points you wrote here ... Great things ... I think you raised some really interesting points. Keep up the good work.

    Best Data Analytics Courses in Bangalore

    ReplyDelete
  25. Very informative message! There is so much information here that can help me thank you for sharing
    Data Analytics Course in Chandigarh

    ReplyDelete
  26. Its content is brilliant in many ways. I think it is an attractive and revealing material. Thank you very much for caring about your content and your readers.

    Business Analytics Course in Ernakulam

    ReplyDelete
  27. I am impressed by the information that you have on this blog. It shows how well you understand this subject.
    Mlops Course

    ReplyDelete
  28. Advance your technical skills required to crack huge datasets to bring out new possibilities from data. Join the Data Science institutes in Bangalore and get access to top industry trainers, LMS, live projects, assignments, and mock interviews to skyrocket your career in the ever- evolving field of Data Science.
    Data Science Training in Bangalore

    ReplyDelete

Post a Comment