Search code examples
resthttpput

Is HTTP PUT with filtering still idempotent?


I’m working on a restful service and I am not sure if what I am doing is valid HTTP. I understand how to use the following HTTP methods: GET, DELETE and POST, but when supporting PUT, I am not so sure. I understand PUT is idempotent but what if I were to use filtering?

For clarity, if you were to use my service, you can:

GET /User

Which would list ALL users.

GET /User/1

Which would get the User who has an ID of 1.

POST /User

Which will create a new User.

PUT /User/2

Which will create a new User with an ID of 2 and if the User already exists, the User will be updated.

Where I start to struggle is when I want to implement filtering on a PUT request. For example, I allow:

PUT /User?FirstName=Andrew&LastName=Schools

This would update the resource that has a FirstName of Andrew and a LastName of Schools. Furthermore, you can also do:

PUT /User?status=1

Which will update any User who has a status of 1 with the contents from the body of the request.

My question is, since I am enabling filtering on a PUT, is this still idempotent? My initial thought is no because the first time you PUT using the filter above, this could change what the same exact filter finds in a subsequent PUT, thus, is NOT idempotent. If the above statement is indeed TRUE, would this feature be better served in a POST?


Solution

  • I don't see a problem. What's relevant is the final state of the resource. If you apply the PUT twice, and on the second request the filter identifies less or no resources, does it change the state of the resources you did want to modify?