About the difference PATCH and PUT

The difference is:
PATCH: update some of the resources, and other non-power, non-security
PUT: update the entire resource, with sex, power and other non-secure
Note:
idempotency: as many as a result of the request and the request once the results of
security: request does not change the state of the resource

For example (my understanding of both definitions) both obvious difference:

Query Resource List

request:
GET /users
response:
[
    {
        "id": 1
        "name": "xx"
        "description": "test xx",
        "phone": "127"
    }
]

This time provides interfaces to support modify the name and description

The first: HTTP interface methods defined PUT:

request:
PUT /users/1
{
    "name": "yy"
}

Query Resource List

request:
GET /users
response:
[
    {
        "id": 1
        "name": "yy"
        "description": null"phone": "127"
    }
]

The second: HTTP method definition interface is PATCH:

request:
PATCH /users/1
{
    "name": "yy"
}

Query Resource List

request:
GET /users
response:
[
    {
        "id": 1
        "name": "yy"
        "description": "test xx""phone": "127"
    }
]

Then there is a question: In the above scenario, PUT and PATCH are idempotent, and why PATCH non-idempotent?
In fact, in the definition regarding PATCH, there is another scenario: PATCH by transmitting a series of instructions, and the type of operation represented by OP

PATCH /users
[
    { 
        "op": "add", 
        "path": "/", 
        "value": {
            "name": "zz",
            "description": "test zz""phone": "128"
        }
    },
    { 
        "op": "replace", 
        "path": "/1", 
        "value": {
            "name": "yy"
        }
    }
]

Query Resource List

request:
GET /users
response:
[
    {
        "id": 1
        "name": "yy"
        "description": "test xx""phone": "127"
    },
    {
        "id": 2
        "name": "zz"
        "description": "test zz""phone": "128"
    }
]

op represents the operation of resources, in total there are six: add, replace, remove, move, copy, test

The above examples are in accordance with the provisions of the PATCH and PUT carried out, for example, the actual results are going to come, just as some people use the POST method to modify the code name based on logic, just say no norms, not to say no.

 

Reference:
https://stackoverflow.com/questions/28459418/rest-api-put-vs-patch-with-real-life-examples

Guess you like

Origin www.cnblogs.com/dj3839/p/11099367.html