Rest API Naming Convention
REST
In REST, primary data representation is called Resource.One of the keys to achieving a good RESTful design is naming the HTTP verbs appropriately. It is really important to create understandable resources that allow people to easily discover and use your services.
It is good practice to expose resources as nouns instead of verbs. Essentially, a resource represents a thing, and that is the reason you should use nouns. Verbs refer to actions, which are used to factor HTTP actions.
But what can I make a resource? Well, these should be nouns that make sense from the perspective of the API consumer.A resource can be a single or a collection.Once you have your resources defined, you need to identify what actions apply to them and how those would map to your API.
Never use CRUD function names in URIs
URIs should not be used to indicate that a CRUD function is performed. URIs should be used to uniquely identify resources and not any action upon them. HTTP request methods should be used to indicate which CRUD function is performed.We should use HTTP method to describe what actions are performed on the resource.
| HTTP Method | CURD Action |
| GET | Read Operation |
| POST | Create Operation |
| PUT | UPDATE |
| DELETE | DELETE |
HTTP GET http://www.netgear.com/device-management/managed-devices //Get all devices
HTTP POST http://www.netgear.com/device-management/managed-devices //Create new Device
HTTP GET http://www.netgear.com/device-management/managed-devices/{id} //Get device for given Id
HTTP PUT http://www.netgear.com/device-management/managed-devices/{id} //Update device for given Id
HTTP DELETE http://www.netgear.com/device-management/managed-devices/{id} //Delete device for given Id
RESTful principles provide strategies to handle CRUD actions using HTTP methods mapped as follows:
- GET /tickets - Retrieves a list of tickets
- GET /tickets/12 - Retrieves a specific ticket
- POST /tickets - Creates a new ticket
- PUT /tickets/12 - Updates ticket #12
- DELETE /tickets/12 - Deletes ticket #12
The great thing about REST is that you're leveraging existing HTTP methods to implement significant functionality on just a single /tickets endpoint. There are no method naming conventions to follow and the URL structure is clean & clear.
Resource Naming
Use Nouns for Naming
For RESTful URI, Nouns are better than verbs (verbs are discouraged).As a rule of thumb, your URI should refer to a thing (a noun) and not an action (verb).The reason for using noun is your API is its ability to represent properties.some examples- User
- User Account
- User’s Order
- Products
Here is the resource URL examplehttp://www.netgear.com/rest/v1/users/{userId}/ordersDo not use RPC way to create your REST API’s
http://www.netgear.com/rest/v1/users/{userId}/orders/{code}
http://www.netgear.com/rest/v1/orders/{code}
2.User Plural Nouns for Naming
You can choose singular nouns for naming your RESTful URI’s but it is recommended to use plurals nouns for naming your RESTful APIs.As one of the most common operations performed by the REST API is to get data, using plural is more intuitive and easy.Keep in mind following points
- Do not mix singular and plural convention while naming your resources.It should be consistent across the API for readability.
- URL represents a resource which can be a singleton or a collection.A plural represents all resources under given URI
/customersrepresent all customers./customers/{id}represents specific customer under this resource.
- Given resource can also contain sub-collections.Think about all the orders placed by the customer in an online shop.
/customers/{id}/orders/customer/{id}/orders/{order-id}
3. Use Lowercase
If possible, use lowercase letter while constructing your URL paths
/customer/{id}/orders/{order-id}
/customer/{id}/addresses/{address-id}
/customer/{id}/addresses/{address-id}
//Not recommended
/customer/{id}/Addresses/{address-id}
/customer/{id}/Addresses/{address-id}
4. Use Hyphens (-)
Use hyphens (-) in case we use long names for building URI.
/customers/{id}/addresses/{address-id}
/customers/{id}/shipping-addresses/{address-id}
/customers/{id}/shipping-addresses/{address-id}
Hyphens improve readability of the long URI.
5. Avoid underscores ( _)
We recommend avoiding underscores while naming your URI. Here are some reasons to do this
- Underscores can create confusion if application front is not correct or may become hidden in some browsers or screens.
Prefer hyphens ( – ) over underscores ( _ )
6. Filter Operations
While building RESTful APIs, we will come across multiple requirements where we need collection of resources with
- Sorting
- Filtering our certain results from the collection.
- Limited results based on certain conditions
- Pagination
Do not create new API for these requirements.Use query parameters to achieve all these results.
http://www.netgear.com/products?catalog=Online
http://www.javadevjournal.com/products?region=USA&brand=dummy&sort=new
http://www.javadevjournal.com/products?region=USA&brand=dummy&sort=new
7.Do not use trailing forward slash (/) in URIs
As the last character within a URI’s path, a forward slash (/) adds no semantic value and may cause confusion. It’s better to drop them completely.
http://www.netgear.com/device-management/managed-devices/ http://www.netgear.com/device-management/managed-devices /*This is much better version*/
8. Do not use file extensions
File extensions look bad and do not add any advantage. Removing them decreases the length of URIs as well. No reason to keep them.
Apart from above reason, if you want to highlight the media type of API using file extension then you should rely on the media type, as communicated through the
Content-Type header, to determine how to process the body’s content.http://www.netgear.com/device-management/managed-devices.xml /*Do not use it*/ http://www.netgear.com/device-management/managed-devices /*This is correct URI*/
Comments
Post a Comment