Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReSTful design: return an empty object as a template for create new form

My question is straighforward - I think.

Currently the following Uris exist:

http://someserver/service/item           GET   returns all items  
http://someserver/service/item           POST  creates a new item  
http://someserver/service/item/{id}      GET   returns item with id {id}  
http://someserver/service/item/{id}      PUT   updates item with {id}  

What I would like to do is return a blank 'item', like a template for creating new items which contains a list of the object parameters, their types, required or not. The reason for this is I would like to build a generic jquery 'create new' plugin completely ignorant of the data structure, which I could apply to all my new objects.

What is the best way to implement this?

I hope this makes sense and thanks for your time.

like image 345
jimjim Avatar asked Dec 07 '22 23:12

jimjim


2 Answers

I understand an answer provided by Darrel but I would respectfully argue against it.

It seems to me that this template object (resource) is an important part of your application because you want to make it generic. It's a first class citizen resource and we're talking about REST, so it should be given a corresponding treatment. I should be able to GET the template resource, it shouldn't be "hidden" behind POST.

GET http://someserver/service/item/template

Then you can also introduce versioning and variability much more easily when you have a resource accessible via GET.

like image 154
Yuriy Zubarev Avatar answered Jan 29 '23 11:01

Yuriy Zubarev


I do pretty much the same thing. I include a link in my "list of items" resource that you can POST to. The response is a template of a new item. Arguably you could also do a GET to retrieve the template, but I use the opportunity to assign a new Id to the item so my request is not idempotent.

like image 35
Darrel Miller Avatar answered Jan 29 '23 11:01

Darrel Miller