CS5_Design of API
CS5_Design of API
Akshaya Ganesan
Assistant Professor[Off-Campus]
BITS-Pilani
Precap
Resource Modelling
• REST APIs embrace all aspects of the HyperText Transfer Protocol including its request
methods, response codes, and message headers
• Each HTTP method has specific, well-defined semantics within the context of a REST API’s
resource model.
• The purpose of GET is to retrieve a representation of a resource’s state.
• HEAD is used to retrieve the metadata associated with the resource’s state.
• PUT should be used to add a new resource to a store or update a resource.
• DELETE removes a resource from its parent.
• POST should be used to create a new resource within a collection and execute controllers.
Interaction Design
Interaction Design with HTTP
• Rule: GET and POST must not be used to tunnel other request methods
• Rule: GET must be used to retrieve a representation of a resource
• Rule: HEAD should be used to retrieve response headers
• Rule: PUT must be used to both insert and update a stored resource
• Rule: PUT must be used to update mutable resources
• Rule: POST must be used to create a new resource in a collection
• Rule: POST must be used to execute controllers
• Rule: DELETE must be used to remove a resource from its parent
• Rule: OPTIONS should be used to retrieve metadata that describes a resource’s available interactions
Interaction Design
HTTP response success code summary
• 200 OK Indicates a nonspecific success
• 201 Created Sent primarily by collections and stores but sometimes also by controllers, to indicate that a new
resource has been created
• 204 No Content Indicates that the body has been intentionally left blank
• 301 Moved Permanently Indicates that a new permanent URI has been assigned to the client’s requested resource
• 303 See Other Sent by controllers to return results that it considers optional
• 401 Unauthorized Sent when the client either provided invalid credentials or forgot to send them
• 402 Forbidden Sent to deny access to a protected resource
• 404 Not Found Sent when the client tried to interact with a URI that the REST API could not map to a resource
• 405 Method Not Allowed Sent when the client tried to interact using an unsupported HTTP method
• 406 Not Acceptable Sent when the client tried to request data in an unsupported media type format
• 500 Internal Server Error Tells the client that the API is having problems of its own
Metadata Design
Important HTTP headers
Media type negotiation should be supported when
• Content type multiple representations are available
• Content length
• Last-modified
• E-tag
• Location
Representation Design
• https://learn.microsoft.com/nl-nl/azure/architecture/best-practices/api-design
References
1) Restful Web services, Leonard Richardson and Sam Ruby, 1 st edition published by O'Reilly
Media,
2) REST API Design Rulebook by Mark Massé
Thank You!
In our next session: