REpresentational State Transfer (REST) is an architectural style that defines a set of constraints to be used for created web services.
- Way of accessing web services in a simple and flexible way without having any processing
- Uses less bandwidth
- Simple, light-weight, highly scalable and maintainable
- Communication is done via HTTP requests Client sends a request to the server which can be GET, POST, PUT, PATCH or DELETE (other methods like OPTIONS and HEAD are rarely used) Server responds back with resources which can be XML, HTML, Image or JSON (JSON is most popular)
POST | Create a resource |
---|---|
GET | Get a resource |
PUT | Replace the resource with the one being sent / create aresource |
PATCH | Update the resource / create a resource |
DELETE | Delete a resource |
Idempotence: An HTTP method is idempotent when the result obtained is the same regardless of the number of times it is executed. For example, A = 4, this statement no matter how many times you execute, the result wonât change but, A++, is not an idempotent request
Architectural Constraints of RESTful API
- Uniform Interface: There should be a uniform way of interacting with the server, irrespective of the device (mobile or web). There are four guidelines of Uniform Interface:
- Resource-based
- Manipulation of resources through representations
- Self descriptive messages
- HATEOAS (Hypermedia As The Engine Of Application State)
- Stateless: Server should not store anything related to the session. All the necessary information required to execute a API call should come from the client
- Cacheable: Every response should include whether the response is cacheable or not. Improves performance and availability
- Client-Server: For a client, the server should be kinda black box
- Layered System: Intermediary servers may improve system availability by enabling load-balancing and by providing shared caches
- Code on Demand: Optional feature, server can provide executable code to the client If a service violates any of the above constraints, it cannot be called RESTful.
DOâS
- Keep base URL simple and intuitive
- There should be only 2 base URLs per resource
- Use HTTP verbs to operate on collections and elements (POST, GET, PUT, DELETE)
- Design the API in such a way that developers probably donât need to look at the documentation
- Use plural nouns for resources
- Use concrete names rather than abstract names. For example, /media is an abstract name while /images /videos /gifs are concrete names and describe a lot more of what the resource is like
- Simplify associations between resources and use attributes with the HTTP question mark (?). For example, GET /dogs?color=black&state=running&location=park
- Use only 7-8 HTTP status codes to describe error, not more than 10
- Make error messages in payload as verbose as possible
- Always release an API with a version
- Versions can be specified as /v1/dogs.
- If response is huge, provide partial response or paginated response
- Developers should be able to add only required fields in the call
- In case of pagination, use limit and offset. For example, a response consists of 4000 objects, so the developer can specify the number of objects requires (limit) and the position from which these object start (offset)
- Make sure to add default values in limit and offset
- API calls that donât actually work on a resource, for example, calculating taxes or converting currencies, for such endpoints, verbs can be used. For example, /convert?from=USD&to=INR
- API should support multiple formats, json, xml etc.
- Follow javascript conventions for naming attributes and use CamelCase
- Consolidate API requests in one subdomain, for example, developers.facebook.com
- For exception handling, there should be an options for suppressing response codes (converting all HTTP response code to 200), for example, /dogs?suppress_response_code=true. In such situation, the actual error HTTP code can move to the payload as response_code
- Use OAuth2.0 for authentication
DONâTS
- Donât build a chatty API
- Donât use verbs in endpoints, for example, /getAllDogsWhoAreHungry
- Donât create too many endpoints
- Donât be an asshole in general when building an API