What is the main difference between PATCH and PUT request?

后端 未结 8 2000
悲&欢浪女
悲&欢浪女 2020-11-28 01:18

I am using a PUT request in my Rails application. Now, a new HTTP verb, PATCH has been implemented by browsers. So, I want to know what the main di

8条回答
  •  攒了一身酷
    2020-11-28 01:55

    I spent couple of hours with google and found the answer here

    PUT => If user can update all or just a portion of the record, use PUT (user controls what gets updated)

    PUT /users/123/email
    new.email@example.org
    

    PATCH => If user can only update a partial record, say just an email address (application controls what can be updated), use PATCH.

    PATCH /users/123
    [description of changes]
    

    Why Patch

    PUT method need more bandwidth or handle full resources instead on partial. So PATCH was introduced to reduce the bandwidth.

    Explanation about PATCH

    PATCH is a method that is not safe, nor idempotent, and allows full and partial updates and side-effects on other resources.

    PATCH is a method which enclosed entity contains a set of instructions describing how a resource currently residing on the origin server should be modified to produce a new version.

    PATCH /users/123
    [
      { "op": "replace", "path": "/email", "value": "new.email@example.org" }
    ]
    

    Here more information about put and patch

提交回复
热议问题