Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Designing RESTful URLs for web pages

Most REST tutorials arranged resources as following:

GET  /car/      -> list of cars
GET  /car/<id>/ -> info about specific car
POST /car/      -> create a new car

but when building web applications for use in browsers, there is a missing link that are rarely discussed, before you can POST to /car/, you need to GET a form for creating a new resource (car). What should the URL for this form be?

I typically used:

 GET  /car/new/ -> form for creating a new car
 POST /car/new/ -> redirect to /car/<id>/ if item is created else show form with invalid fields highlighted

but according to http://www.slideshare.net/Wombert/phpjp-urls-rest this is not a good REST URL. I can see why it's not a good REST, because "new" is really used as a verb and not a resource, but where should the form be then, because GET /car/ is already used for listing cars, so you can't use GET /car/ for the form for new cars.

In short, my question is: "What is the RESTful URL for 'create resource form'?"

On a slightly related note, even in a web service, it is sometimes not always wise to rely on the client knowing the schema in advance, therefore even in web services there could be a need to be a way for client to request the resource's current schema. AFAICS, this as a similar situation with the need to GET a create form (i.e. the form is sort of like a schema which describes how to construct POST query for creating the resource). Is my line of thought here correct?

like image 615
Lie Ryan Avatar asked Oct 07 '22 13:10

Lie Ryan


1 Answers

REST does not care too much about what your URI looks like as long as it identifies one unique resource and is self-describing. Meet those criteria, and beyond that, it is personal preference. Nothing prohibits using a verb in a URI if it makes sense to use one.

In regards to your slightly related note, what you hint at with the form being a schema is media type. RESTful architecture concerns the client and the server both understanding the media types used to represent the application state.

A REST API should spend almost all of its descriptive effort in defining the media type(s) used for representing resources and driving application state, or in defining extended relation names and/or hypertext-enabled mark-up for existing standard media types. Any effort spent describing what methods to use on what URIs of interest should be entirely defined within the scope of the processing rules for a media type (and, in most cases, already defined by existing media types).

Read more here: http://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven

That is from Roy Fielding, the man who defined REST. In general, your media types should be extensible -- that is, any changes should add on and not break older clients unless necessary.

like image 64
tuespetre Avatar answered Oct 11 '22 01:10

tuespetre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!