Thank you for taking the time to read this. I looked over Stack Overflow and didn’t see a similar question, but if I missed one I apologize.
So I’m building a RESTful API, along with a user interface that is the (first) consumer of said REST API.
I have a need to create an object. The creation of that object takes a decent amount of configuration.
I understand about the verb I need to use (POST – let’s not argue this point) and the URL path that I need to have. My question is about how to configure the parameters. Let me give you a sample configuration object in JSON format so as to be agnostic:
{
name: "foo",
barid:1,
features:[
{
id:1,
config: {
foo:bar,
fubar:baz
}
},
{
id:2,
config: {
foo:bar,
fubar:baz
}
},...
]
}
So what I’m wondering is this… what’s the correct way to POST the features parameter? Should I just make “features” a JSON string and deserialize it on the server side? Is there some convention that you follow to build an array of nested objects in a RESTful POST? Other developers are going to have to consume this, and I certainly don’t want people to have to hand-construct what jQuery did with my JSON object, which looked like:
features[0][featureid]:2
features[0][configuration][min]:-64
features[0][configuration][max]:50
features[0][include]:true
features[1][featureid]:3
features[1][configuration][min]:0
features[1][configuration][max]:80.5
features[1][include]:true
My grails app didn’t like that very much 🙂
Thanks again for your time.
Grails can accept parameters like
features[0].featured=2&features[0].configuration.min=-64, but you must prepare empty structures and features array before actual mapping. It would be:+
lazyListcreates list elements on demand. If you know exactly amount of features – you can fill it manually w/o using lazyList.Btw, it’s also a good way to POST/PUT actual JSON in body to server, instead of request parameters. It’s default way of BackboneJS, for example. So you will be able to parse incoming JSON by yourself, of map to same commands (requires little configuration).