Friday, July 19, 2019

HTTP status code standard of restful API

While designing the restful API, we need to make sure that API should always return the right and consistence HTTP status Code and without consistent HTTP status codes, customers will not know the difference between success or failure without parsing the response body.

HTTP standard provides almost 70 status codes to describe the response status and you can use below HTTP status code for restful API.

200 – OK
204 – OK – No Content
400 – Bad Request
401 – Unauthorized
500 – Internal Server Error
503 – Service is not available

HTTP – GET (Resource Inquiry):

HTTP GET Sequence Diagram
HTTP GET Sequence Diagram

The above sequence diagram explain how the HTTP request is being processed and returns the HTTP status code.

There are the possible scenarios for processing of the HTTP GET request.

1.    Scenario :   If request resource is found, Returns HTTP – 200 OK with Resource data

HTTP GET 200 OK

2.    Scenario:   If request resource is not found, Returns HTTP – 200 OK with NULL data.

HTTP GET 200 OK
           
3.    Scenario:       Any Validation Error/invalid input – returns HTTP – 400 Bad Request with validation or error message.

             
HTTP GET 400 BAD REQUEST


HTTP – POST (Resource Creation) 

HTTP POST Sequence Diagram
HTTP POST Sequence Diagram

The above sequence diagram explain how the HTTP request is being processed and returns the right HTTP status code.

There are the possible scenarios for processing of the HTTP POST request.

1.    Scenario:   If requested resource is successfully created, Returns HTTP – 201 OK with Newly created Resource data

HTTP POST 201 OK
2.    Scenario:

If request resource is already found or duplicate resource, returns – returns HTTP – 400 Bad Request with validation message

                                                                               OR

·              Any validation or invalid input error - returns HTTP – 400 Bad Request with validation message

HTTP POST 400 BAD REQUEST
  
   HTTP – PUT (Resource Update)

HTTP PUT Sequence Diagram
HTTP PUT Sequence Diagram


The above sequence diagram explain how the HTTP request is being processed and returns the right HTTP status code.

There are the possible scenarios for processing of the HTTP PUT request.

1.    Scenario:   Any validation or invalid input error - returns HTTP – 400 Bad Request with validation message
           
HTTP PUT 400 BAD REQUEST


2.    Scenario:  If request resource is not available, return HTTP – 400 Bad Request – with message “Resource is not available

HTTP PUT 400 BAD REQUEST

In this scenario, if resource is not available, many architect prefer to create new resource and returns HTTP – 201 OK with newly created resource data

HTTP PUT 201 OK
        
3.    Scenario:  If request resource is available and successfully updated, Returns HTTP – 200 OK with updated Resource data
     

HTTP PUT 200 OK


HTTP – Patch (Partially Resource Update)


HTTP PATCH Sequence Diagram
HTTP PATCH Sequence Diagram

The above sequence diagram explain how the HTTP request is being processed and returns the right HTTP status code.

There are the possible scenarios for processing of the HTTP PATCH request.

1.    Scenario : If request resource is available and successfully updated , Returns HTTP – 200 OK with updated Resource data

HTTP PATCH 200 OK
         

2.    Scenario :  Any validation or invalid input error - returns HTTP – 400 Bad Request with validation message

HTTP PATCH 400 BAD REQUEST
                                                         
3.    Scenario :  If request resource is not available, return HTTP – 400 Bad Request – with message “Resource is not available

                
HTTP PATCH 400 BAD REQUEST




HTTP – DELETE (Delete Resource)

HTTP DELETE Sequence Diagram
HTTP DELETE Sequence Diagram

The above sequence diagram explain how the HTTP request is being processed and returns the right HTTP status code.

There are the possible scenarios for processing of the HTTP DELETE request.

1.    Scenario : If request resource is available and successfully deleted, Returns HTTP – 200 OK 

HTTP DELETE 200 OK


2.     Scenario :  Any validation or invalid input error - returns HTTP – 400 Bad Request with validation message


HTTP DELETE 400 BAD REQUEST

3.    Scenario :  If request resource is not available, return HTTP – 400 Bad Request – with message “Resource is not available


HTTP DELETE 400 BAD REQUEST


Note: In some scenario, requested URI is not matching with any API URI., then by default Web API/IIS returns HTTP – 404 - with message “HTTP resource was found that matches the request URI”

References:
 
Thanks for Visiting!

No comments:

Post a Comment

Setup Swagger for ASP.Net Web API

this blog demonstrates step by step to add swagger in web api project and will submit  http request GET/POST/PUT via swagger UI. Swagger ...