PUT() AND PATCH() IN REST .
To understand PUT and PATCH let us consider one table USER . In user table we have three columns . user_name , email and phone_num .
USER
-----------
user_name
| varchar(45)
|
email
| varchar(40) |
phone_num | varchar(30) |
When we try to get data using in JSON format rest call using GET()
## /users/1
{
"user_name": "skwee357",
"email": "skwee357@domain.com"
"phone_num": "1234567890"
}
Now we want to email then using PUT when need send all column data including
new email data we want to change.
PUT /users/1
{
"username": "skwee357",
"email": "skwee357@gmail.com"
"phone_num": "1234567890" // new email address
}
but if we use patch . Simple no need to send all column data we will send email .
PATCH /users/1
{
"email": "skwee357@gmail.com" // new email address
}
Now we can conclude .
Method PUT --
1. PUT is idempotent.
2. PUT update all row data.
3. We need to send all row data , even if want to update single column of the row.
Use PUT APIs primarily to update existing resource (if the resource does not exist, then API may decide to create a new resource or not). If a new resource has been created by the PUT API, the origin server MUST inform the user agent via the HTTP response code 201 (Created) response and if an existing resource is modified, either the 200 (OK) or 204 (No Content) response codes SHOULD be sent to indicate successful completion of the request.
If the request passes through a cache and the Request-URI identifies one or more currently cached entities, those entries SHOULD be treated as stale. Responses to this method are not cacheable.
PATCH
1. PATCH is not idempotent.
2. PATCH use for partial update of the column of any row .
3. We need to send only those column data we want to update in a row .
Request has body ----------Yes
Successful response has body --- Yes
Safe -- No
Idempotent --------- No
Cacheable----------------No
Allowed in HTML forms -----------No
|

No comments:
Add your comment