Zada Zorg
Zada Zorg

Reputation: 2818

REST: post vs posts & new vs create

When you design URL schema for your application, which rules you use?

I'm prefered: /post/ for list, /post/23/ for details, /post/23/edit/ for editing, just b/c I'm can very easy work with that URL by hand in browser location bar. I'm wrong? Suggest me, please.

Thanks.

Upvotes: 5

Views: 4513

Answers (2)

VolCh
VolCh

Reputation: 369

I use:

  • GET /posts for list
  • GET /posts/new to get empty HTML form for 'static' sites only ('ajax' sites and 'stand-alone' clients don't need it)
  • POST /posts for create ('action' attr in previous form)
  • GET /posts/23 for detail (not for editing!)
  • GET /posts/23/edit to get filled HTML form with hidden '_method=PUT' field to emulate PUT in browsers
  • PUT /posts/23 for editing ('action' attr in previous form)
  • DELETE /posts/23 for deleting ('inline' HTML form with '_method=DELETE' to emulate)
  • POST /posts/23 for emulate PUT and DELETE in browsers

And I never use /posts/ or /posts/23/ because it makes hard (or dirty) to change response format. I can use /posts (synonym for /posts.html) for browsers , /posts.xml for XML services, /posts.json for 'ajax' data and /posts.smth_else for smth else :) in future. Also all of them may be static files (cache or archive) to free CPU and memory on high load.

Upvotes: 0

labratmatt
labratmatt

Reputation: 1841

You should probably use the HTTP POST method when creating a new resource. So, for a new customer, you might POST to example.com/customer. Then if you want information on that customer, do a GET to example.com/customer/{your recently created customer id}. If you want all customers, do a GET to example.com/customer If you want to edit a customer you'll probably want to PUT to example.com/customer/{your customer id}

It seems that your fundamental issue is that you are dealing with is that you are specifying your action (or verb) in your URL. You don't need to do that. Instead of doing something like example.com/edit/23, you should use the HTTP PUT method with example.com/23 (or example.com/customers/23).

Have a look at what is RESTful/REST for a review on creating RESTful resources.

Have a look at PUT vs POST in REST for the difference between POST and PUT (edit and create).

For building more complex RESTful URLs, I generally refer to this presentation from the LinkedIn nerds.

Upvotes: 4

Related Questions