John Smith
John Smith

Reputation: 1336

HTTP POST instead of CRUD REST paradigm

I have an question: how can I use POST HTTP method to simulate CRUD paradigm. I know that I can use POST to update and create but how can I delete and retrieve a resource using POST?

Upvotes: 1

Views: 319

Answers (2)

Alberto Zaccagni
Alberto Zaccagni

Reputation: 31560

To use REST you should

  • POST when you want to create an item
  • DELETE when you want to remove it
  • PUT when you want to update it
  • GET when you want to retrieve it

It doesn't make sense to POST to a url to GET data from it, that will confuse your users.

For example, with jquery with the following you create a user

$.ajax({
  url: 'http://example.com/users, 
  type: 'POST',
  data: user
})

...and then you could update it

$.ajax({
  url: 'http://example.com/users, 
  type: 'PUT',
  data: user
})

...or maybe delete it

$.ajax({
  url: 'http://example.com/users, 
  type: 'DELETE'
})

and try to GET it to see if it's still there

$.ajax({
  url: 'http://example.com/users
})

(I omitted the callbacks for simplicity)

Upvotes: 1

Mateusz
Mateusz

Reputation: 2317

HTTP has DELETE and GET so you should not use POST for such actions.

https://www.rfc-editor.org/rfc/rfc2616

Upvotes: 1

Related Questions