0

POST to create a new item, PUT to update an item.. but what if there's only one item in the database?

Do I code: 'if item already exists, find it and PUT' else 'POST'

or am I missing something very obvious?

I briefly thought of doing post everytime, and on the first line of the post just delete everything, then post, but that seems dumb

azium
  • 20,056
  • 7
  • 57
  • 79
  • Related: [In REST is POST or PUT best suited for upsert operation?](http://stackoverflow.com/questions/18470588/in-rest-is-post-or-put-best-suited-for-upsert-operation) – Jonathan Lonowski Oct 14 '14 at 02:18

1 Answers1

0

Personally, I prefer to POST for the original creation, and PATCH for updates...


If you have the id, or can generate it on the client (such as a uuid):

PUT /myresources/:id

If you don't have the id, or need to generate it on the server:

POST /myresources

should return a location header with the generated path, as well as an acceptable response body.

{
  id: "someid",
  location: "/myresources/someid"
}
Tracker1
  • 19,103
  • 12
  • 80
  • 106
  • This doesn't really answer the question (at least as far as my read of it goes). The question Jonathan linked to is identical. – Brad Oct 14 '14 at 02:28