What is the main difference between PATCH and PUT request?

后端 未结 8 2017
悲&欢浪女
悲&欢浪女 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:46

    Here are the difference between POST, PUT and PATCH methods of a HTTP protocol.

    POST

    A HTTP.POST method always creates a new resource on the server. Its a non-idempotent request i.e. if user hits same requests 2 times it would create another new resource if there is no constraint.

    http post method is like a INSERT query in SQL which always creates a new record in database.

    Example: Use POST method to save new user, order etc where backend server decides the resource id for new resource.

    PUT

    In HTTP.PUT method the resource is first identified from the URL and if it exists then it is updated otherwise a new resource is created. When the target resource exists it overwrites that resource with a complete new body. That is HTTP.PUT method is used to CREATE or UPDATE a resource.

    http put method is like a MERGE query in SQL which inserts or updates a record depending upon whether the given record exists.

    PUT request is idempotent i.e. hitting the same requests twice would update the existing recording (No new record created). In PUT method the resource id is decided by the client and provided in the request url.

    Example: Use PUT method to update existing user or order.

    PATCH

    A HTTP.PATCH method is used for partial modifications to a resource i.e. delta updates.

    http patch method is like a UPDATE query in SQL which sets or updates selected columns only and not the whole row.

    Example: You could use PATCH method to update order status.

    PATCH /api/users/40450236/order/10234557

    Request Body: {status: 'Delivered'}

提交回复
热议问题