While writing code to consume Restful Web API's, as well as seeing some of the Restful Web API's written in some of the companies I've worked with, I get frustrated at times. I see a lot of implementations where basic, simple principles of a Restful Service are not followed. The 2 common issues that i see a lot are
- Incorrect HTTP Verb is used. HTTP Post is used all the time. Even when it is to Get or Delete a resource, a method/url called GetCustomer or DeleteCustomer will be written and it expects a request Posted to it.
- Incorrect HTTP Status Codes are returned. Sometimes Status Code 200 is returned all the time with the error message in the response body.
I thought I'd write a quick blog as a reference to how the Restful Web Services should be implemented, the verbs which should be used, and the different HTTP Status Codes returned for different scenarios.
Let's assume i have a Restful Web Api, called http://mywebapi.com/Api. It can be used to create/update/get/delete Customers.
Get a Customer
Http Verb: Get
- If a customer with the provided id is not found, return HTTP Status Code of 404 (Not Found)
- If a customer is found, return customer with HTTP Status Code of 200 (OK).
Get a List of Customer
Http Verb: Get
- Return the list of Customer with HTTP Status Code of 200 (OK). (If no customer was found, the list will just be empty.)
Create a Customer:
URL: http://mywebapi.com/api/customer
Http Verb: Post
HTTP Request Body: The Customer object in Json format.
- If the Customer object can not be read/parsed from the HTTP Request Body, return HTTP Status code of 400 (Bad Request).
- If the Customer object can not be created/saved in the DB, Http Status Code of 500 (Internal Server Error)
- If the Customer object is created, return HTTP Status Code of 201 (Created) + ideally the url of the new customer, for e.g. http://mywebapi.com/api/customer/104
Update a Customer:
URL: http://mywebapi.com/api/customer/{customerId}
Http Verb: Put
HTTP Request Body: The Customer object in Json format.
- If the Customer object can not be read/parsed from the HTTP Request Body, return HTTP Status code of 400 (Bad Request).
- If a customer with the provided id is not found, return HTTP Status Code of 404 (Not Found)
- If the Customer object can not be updated/saved in the DB, Http Status Code of 500 (Internal Server Error)
- If the Customer object is created, return HTTP Status Code of 204 (No Content)
Delete a Customer
URL: http://mywebapi.com/api/customer/{customerId}
Http Verb: Delete
- If a customer with the provided id is not found, return HTTP Status Code of 404 (Not Found)
- If the Customer object can not be updated/saved in the DB, Http Status Code of 500 (Internal Server Error)
- Return HTTP Status Code of 200 (OK) + ideally the Customer Object which was deleted.
Note:
SOAP, which used to be the primary standard for Message and Data exchange on the web, has been steadily replaced by Restful Services in the last few years, largely owing to it's simple design.
In the .Net world, ASMX and WCF were used for creating SOAP Web Services and nowadays, ASP.Net MVC Web API is used for creating Restful Web Services.