Domanda

I have a scenario where I have to either

  • Pull the data from backend as search
  • Pull the same data from backend to administrate

The URLs I am using are -

  • /cars
  • /cars/management

The search can be then subsequently filtered as

  • /cars?color=blue

The concern that I have is that management is not a resource - it is actually an action. The management page contains links for other functionality associated with car management [add a car/delete a car/list cars/modify cars etc]

Has somebody else faced this issue? Can you let me know your solution?

È stato utile?

Soluzione

There is nothing wrong with having a management resource that is a page showing management options for cars. Just because "cars" is the only thing in your database doesn't mean that is the only resource you can present to the user. You could have a resource that is just a form to pick a color (that makes a POST or PUT to the car resource). You could have a resource that is just a form to fill out the address you want the car delivered to. You could have a resource that is just a check box whether you want leather seats or not. You can have as many resources as you like and that make sense, even if all the resources are are pages with forms or links back to the car resource.

Just don't put any verbs in your URLs. You should still be using state transfer using HTTP verbs to change the state of the resources. Don't have a link like

GET /cars/123/deleteCar

on the management page. Instead there might be a link on the management page that (probably using Javascript since browsers have poor native support for HTTP verbs) performs a HTTP request along the lines of

DELETE /cars/123

when the user clicks the link. Something like jQuery can help with that. So long as the management page is using the HTTP verbs to change the state of the resources you are following REST since HTTP is a REST constrained protocol. REST doesn't say don't have actions, it says the actions should be constrained to state transfer.

Altri suggerimenti

  1. There is no such concept as a "RESTful URL";
  2. There is no issue with your URLs(/cars, /cars/management)
  3. "/cars/management" is valid resource and it is not an action at all.

The RESTfull way to do this is to use the same URL with different HTTP verbs:

  • GET /cars for search/listing.
  • POST /cars for insert.
  • PUT /cars?id=123 (or /cars/123) for update.
  • DELETE /cars?id=123 for delete.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top