REST API development is very popular today, fulfilling rapid growing of cloud services and apps. You know, one of REST architectural constraints is Uniform Interface - stating that developers should use common, well-known HTTP methods and status codes in their APIs, in a way that ensures conformity across the web.

So what is the best practice widely used by the industry? In this article, I’d like to share with you guys how to use the right HTTP methods and status codes in your REST APIs.

 

1. How to use the Right HTTP Methods for REST APIs

Basically, for CRUD operations (Create, Retrieve, Update and Delete) you can use the HTTP methods as follows:

- POST: create resource or search operation

- GET: read resource operation

- PUT: update resource operation

- DELETE: remove resource operation

- PATCH: partial update resource operation



Let’s go into detail of each HTTP method.

 

API end point with POST method:

Used mainly for Create resource operations, or Read operations in some certain cases:

Return status code:

 

API end point with GET method:

Used primary for Read resource operations.

Return status code:

 

API end point with PUT method:

Used for Update resource operations.

Return status code:

 

API end point with DELETE method:

Used for Delete resource operations.

Return status code: 204 No Content  for successful delete operation.

 

API end point with PATCH method:

Used for Partial update resource operations.

Return status code: 200 OKfor successful partial update operation.


2. How to use the Right Status Codes for REST APIs

Returning the right HTTP status codes in REST APIs is also important, to ensure uniform interface architectural constraint. Besides the status codes above, below is the guideline for common HTTP status codes:


3. Notes

There’s no strict rule that specifies which method should be used for which operation. However, it’s better to follow some certain rules widely used by the industry, as mentioned above.

And be flexible, you can break the rule in certain cases. It’s not strict requirement.

 

Related REST API Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.