RESTful API Design
JSON
Number: 123
Boolean: true/false
Null: null
String: "abc"
Object: {"attribute":123, "attribute2":true, "attreibute3":{"A":65. "B":66}}
Array: [123, "abc",98]
API Version
option1: Add API version in path
option2: Add API version in http header
CRUD: HTTP verbs
Create(POST), Read(GET), Update(PUT), Delete(DELETE), payload: json
http status codes
200: ok 201: created 204: No Content
304: Not Modified
400: Bad Request
401: Not Authorized
404: Not Found
500: Internal Server Error
>>> Create a User
POST /users
request:
{
"username": "Chang"
"User Status": "Gold"
}
or
request:
{
"user ID": 17,
"Username": "Chang",
"usrt Status": "Gold"
}
>>> Get All Users
GET /users
request:
response:
[
{ "user id":1, "username": "chang", "user status":"gold"},
{ "user id":2, "username": "lee", "user status":"silver"},
]
>>> Get a user with a given ID
GET /users/{userid}
response: { "user id":1, "username": "chang", "user status":"gold"}
>>> Update a user
PUT /users/{userid}
request: {"user id":1, "username": "chang", "user status":"silver"}
response: {"user id":1, "username": "chang", "user status":"silver"}
>>> Delete a user
DELETE /users/{userid}
request:
response: 204
>>> Friends
POST /users/{userid}/friends
GET /users/{userid}/friends
GET /user/{userid}/friends/{friendid}
/friends/{friendid}
PUT /users/{userid}friend/{friendid}
/friend/{friendid}
DELETE /user/{userid}/friend/{friendid}
/friend/{friendid}