Client
Web API
Backend
request
response
POST - GET - PUT - DELETE
CREATE - READ - UPDATE - DELETE
Fetch resource collection
GET /api/v1/users
HTTP Status : 200 OK
response headers
response
Fetch single resource
GET /api/v1/users/1211 -- /api/v1/users/{id}
HTTP Status : 200 OK
response headers
response
Fetch single response headers
HEAD /api/v1/users/[{id}]
HTTP Status : 200 OK
response headers
Create a resource
POST /api/v1/users
Request Headers
Request body
HTTP Status : 201 CREATED
response headers
response
Modify a resource
PUT /api/v1/users/{id}
Request Headers
HTTP Status : 200 OK
response headers
response
Delete a resource
DELETE /api/v1/users/{id}
Request Headers
Request body
HTTP Status : 200 OK
response headers
How to model associations
All employees in a department
GET /api/v1/departments/{id}/employees
Get an employee in a department
GET /api/v1/departments/{id}/employees/{empId}
GET, HEAD should not modify resource and must not have any side effects
Never expose any unsafe operations using GET
GET, HEAD PUT, DELETE, OPTIONS are idempotent
POST is not
Use controller pattern - verbs in the URI
Example
/login
/logout
/resetPassword
Use Query Parameter
GET /api/v1/departments?q="{'name':'finance'}"&page=1&limit=50&order=asc
Incorporate version in URI
POST /api/v1/departments
Use HTTP Headers to reduce both client and server processing time
Response Header - ETag, Last-Modified
Request Header - If-Modified-Since, If-None-Match
API Design is a must
Spend time on proper API Design
Use tools such as Apiary, API-Blueprint, Swagger etc
Good documentation acts as contract for api consumers
Identify Resources
Identify Resource Representational Format
Identify Supported Methods
Identify Required request, response Headers
Identify Return Status Codes
Resources can be served in different representations
XML, JSON, HTML, CSV ...
Methods
Request Header - Accept, Content-Type
Query Parameter GET /api/v1/departments?format=json
URI Extension GET /api/v1/departments.json
Curl
frisbyjs (node)
junit
Postman
Overusing POST
Actions in URI
Service as a resource
Server managed sessions