0

I want to create an application which is similar to this - A client (identified by user id) sends a REST API call to the server. The server queries database to check if this user has a badge before. If it has, it doesn't do anything. Otherwise, it marks in database that the user is granted the badge and it sends an email to the user.

PUT /user//badge/ POST /user//badge/

My question here is which http method should I use here? PUT or POST?

It is idempotent in the sense that email is sent in only the first request and subsequent requests don't do anything other than querying the db.

What http method do books recommend to be used in this case?

Neeraj Gupta
  • 765
  • 1
  • 10
  • 18

1 Answers1

0

If it's idempotent then it should normally be a PUT, as is clear from from RFC 7231, section 4.3.4:

The fundamental difference between the POST and PUT methods is highlighted by the different intent for the enclosed representation. The target resource in a POST request is intended to handle the enclosed representation according to the resource's own semantics, whereas the enclosed representation in a PUT request is defined as replacing the state of the target resource. Hence, the intent of PUT is idempotent and visible to intermediaries, even though the exact effect is only known by the origin server.

Community
  • 1
  • 1
ma499
  • 601
  • 4
  • 13