0

I am building a web services application and I am looking for advice on how to update a resource.

user API

I have a user endpoint which allows the creation, modification and ability to delete a user in the database. My question relates to the updating of the user. In the table I have written a schema of how the API would work and the descriptions of each endpoint from what I have read.

The PUT and POST of an existingEmailId appear to do the same thing because the emailId is the actual id of the resource. Therefore for this endpoint is it appropriate to just create one of the endpoints, or should I be using both?

Any advice would be appreciated.

SJC
  • 2,767
  • 4
  • 20
  • 31

3 Answers3

1

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.

-- RFC 7231

If you have access to all the information required to create a new user, including any relevant unique IDs, then you should prefer to PUT the full representation up to the server. I would not support both PUT and POST - why have two methods that do the same thing? What if you need POST later for some other action?

Return a 201 Created if the PUT results in a new resource, and a 200 OK if the PUT updates an existing resource.

Community
  • 1
  • 1
Eric Stein
  • 13,209
  • 3
  • 37
  • 52
0

PUT is used for updating resources, as PUT should be idempotent.

JasonZhao
  • 82
  • 8
-1

POST should be used for the insertion of new data, whereas PUT should be used to update.

http://www.restapitutorial.com/lessons/httpmethods.html

  • I have been referring to this other answer on SO: which says the opposite. http://stackoverflow.com/questions/630453/put-vs-post-in-rest – SJC Mar 02 '16 at 09:03
  • Seems to be a bit of split opinion on the subject. I think the comment regarding **POST** being sent to /email (for example) without knowledge of the resource in question, compared to **PUT** being sent to /email/some-email-id, i.e. **with** knowledge of the resource in question, is the most poignant. – Richard Bourne Mar 02 '16 at 09:29