Sunny Gupta
Sunny Gupta

Reputation: 7067

GET, PUT, DELETE, HEAD : What do you mean by Idempotent

I was listening a video on youtube on REST API, below is the link:

REST API

It is said that GET, PUT, DELETE, HEAD are idempotent operations i.e. you can invoke them multiple times and get the same state on the server.

Would anybody please elaborate this line?

Upvotes: 0

Views: 749

Answers (2)

user177800
user177800

Reputation:

Just what it says

No matter how many times that Resource is requested with the exact same URL, the state on the server will never change as a side effect because of the request.

idempotent:

Denoting an element of a set that is unchanged in value when multiplied or otherwise operated on by itself

The point is multiple requests that are exactly the same:

So if you request an image from a server 1000 times with the same URL, nothing on the server is changed.

If you call DELETE multiple times on the same resource, they state on the server doesn't change. This removes the resource, and nothing else, no side effect. And if the resource is not there, good that is what we wanted and nothing else should be affected on the server.

Those Verbs should never have side effects.

Doing a GET should not cause side effects to alter the state of the server no matter how many times this exact URL is requested.

It is about repeated subsequent calls

Example:

Calling GET on a resource should NOT modify a database record, or cause any changes. If it does it isn't following the rules.

If you call HEAD on a resource 1000 times in a row, the state on the server should not change. It might return different data because some removed the resource separately, but repeated calls should never do anything different on the server.

What is NOT idempotent

Example:

Calling GET multiple times causes a counter on tracking that resource to increment every time you make the request with the exact same URL. This is not idempotent. There is a side effect and the state of the server is changing because of the request.

Upvotes: 1

rexcfnghk
rexcfnghk

Reputation: 15492

Idempotent means no matter how many times you invoke that method (e.g. GET), you won't introduce side effects. For example when you issue a GET request to a URL (navigating to http://www.google.com in a browser), you theoretically won't change the state of the web server no matter how many GET requests you issued to the server.

As a real world example, you shouldn't allow some database DELETE/INSERT operations be accessible via an HTTP GET. There are numerous accidents for the Google Crawler to accidentally deleted entities from databases while crawling (i.e. GETting) websites.

Upvotes: 0

Related Questions