2

I have a SPA (single page application) so it uses AJAX extensively for getting and saving data to and from the server. In one case I allow the admin to view/add/edit/delete users. Some current urls for this area looked something like:

(GET) /users?userId=1   // get user with id of 1
(POST) /users?userId=1&firstName=Jim    // update the first name of the user with id 1
(POST) /users?firstName=Bob    // create a new use with the first name Bob
(POST) /users?userId=1&delete=true    // delete user with id of 1

Having spent some time working on a RESTful API in a related project, I'm wondering if it's preferred to use HTTP types (GET, POST, PUT, DELETE) in a web app as well. Also, is it better to use a path parameter for user id instead of a query parameter? So are these urls (rewrite of the ones above) a better option in the long run:

(GET) /users/1   // get user with id of 1
(PUT) /users/1?firstName=Jim    // update the first name of the user with id 1
(POST) /users?firstName=Bob    // create a new use with the first name Bob
(DELETE) /users/1    // delete user with id of 1
Jeremy
  • 1,023
  • 3
  • 18
  • 33

1 Answers1

1

In theory yes you should. You should be as RESTful as possible which means using HTTP semantics to their fullest. However the reality is a bit more murky, several older browsers, I don't need to name names, don't support anything but GET and POST. So the current recommendation until those browsers go out of support, or until you drop support for those browsers, is to have backup methods that do the same thing but on POST, usually with an extra parameter or segment in the url.

Mgetz
  • 5,108
  • 2
  • 33
  • 51
  • I should be clear that I'm not designing a RESTful API in this case, but just a typical SPA web app with lots of AJAX. I need to support IE 8 and higher along with modern versions of Firefox and Chrome. Will those browsers support PUT and DELETE through AJAX? I know html forms are a different story. – Jeremy Jun 29 '13 at 18:40
  • It appears that insofar as the browser supports native `XMLHTTPRequest` it should work fine in AJAX. http://stackoverflow.com/questions/165779/are-the-put-delete-head-etc-methods-available-in-most-web-browsers – Mgetz Jul 01 '13 at 14:53