What is the difference between POST, PUT and PATCH?

There is a lot of confusion between the exact differences between POST PUT and PATCH in REST API.

Let’s take the example of the user resource in an API. We have GET, POST, PUT and PATCH methods for each of the user records.

POST

POST method is used to create a new resource in a collection. In this case, we use POST to insert a new user to the collection. POST is not idempotent, ie If the same request is sent multiple times, multiple records get created.

eg

This will create a new user with username ‘user1’ and email ‘user1@axatrikx.com’ with id of 1. Below, you can see the response of a GET call

PUT

PUT method is used to update an existing record or create one if it doesn’t exists. This replaces the record with the values provided (or not provided) in the request. ie. its same as deleting the record, and then creating a new record in its place with the values provided. So if few fields were not provided in the PUT request, they will not be present in the updated resource. Let’s see two scenarios for updation.

We provide all the available field

This will update the username and email of the record

We do not provide all the fields

This will update the email value, but will set the username as null. So a GET request will return this.

SEE ALSO:   Selenium WebDriver Cheat Sheet

PUT is idempotent, ie If we sent the same request several time, it doesn’t affect the resources as it will just update the same record with same values.

PATCH

PATCH on the other hand only update the fields given, will retain the other values as such. So this request will just update the email and keep the username unchanged. PATCH isn’t required to be idempotent and so is considered not idempotent.

Summary

POSTCreates a new resource
PUTReplaces an existing resources or create if it doesn’t exist
PATCHUpdates an existing resources with the values given in request

For the best REST API Testing tools, you can check this post.

You may also like...

1 Response

  1. Beatriz says:

    Hurrah!After all I got a weblog from where I can really
    obtain helpcul facts concerning my study and knowledge.

Leave a Reply

Your email address will not be published. Required fields are marked *