HTTP methods: GET, POST, PUT, PATCH, DELETE
POST: create a new resource
POST /users
{
"name": "Alice",
"age": 20
}
GET: retrieve a resource
GET /users/0
# Result is:
{
"name": "Alice",
"age": 20
}
DELETE: delete a resource
DELETE /users/0
PUT: replace or create a entire resource
PUT /users/0
{
"name": "Alice",
"age": 25
}
If resource user
with id
= 0
not exist, this PUT
command will behave like POST
command.
Because this is replacing, we must specify all attributes in PUT body. Missing attributes will be lost, like this request
PUT /users/0
{
"age": 25
}
GET /users/0
# Result is:
{
"age": 20
}
Attribute “name” is lost.
PATCH: replace some attributes of a resource
We use PATCH to change some attributes. Other attributes will remain the same.
PATCH /users/0
{
"age": 25
}
GET /users/0
# Result is:
{
"name": "Alice",
"age": 20
}