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

 


HTTP METHOD

CRUD

ENTIRE COLLECTION (E.G. /USERS)

SPECIFIC ITEM (E.G. /USERS/123)

POST

Create

201 (Created), ‘Location’ header with link to /users/{id} containing new ID.

Avoid using POST on single resource

GET

Read

200 (OK), list of users. Use pagination, sorting and filtering to navigate big lists.

200 (OK), single user. 404 (Not Found), if ID not found or invalid.

PUT

Update/Replace

405 (Method not allowed), unless you want to update every resource in the entire collection of resource.

200 (OK) or 204 (No Content). Use 404 (Not Found), if ID not found or invalid.

PATCH

Partial Update/Modify

405 (Method not allowed), unless you want to modify the collection itself.

200 (OK) or 204 (No Content). Use 404 (Not Found), if ID not found or invalid.

DELETE

Delete

405 (Method not allowed), unless you want to delete the whole collection — use with caution.

200 (OK). 404 (Not Found), if ID not found or invalid.

 

PUT() AND PATCH() IN REST . PUT()  AND PATCH()  IN REST . Reviewed by Mukesh Jha on 12:49 AM Rating: 5

No comments:

Add your comment

All Right Reserved To Mukesh Jha.. Theme images by Jason Morrow. Powered by Blogger.