I’m still very new to web development, so please bear with me if this question is really stupid.
I’m trying to follow a REST principles, however, I really appreciate how JSON flows with Ruby and anything that is connecting externally through an API. I know that its pretty standard in REST to input parameters directly into the URL like so: myurl.com/ExampleModel/MyIDParameter and send the JSON (if there is any) to that URI, right?
What I would like to know is: Is it against REST principles to remove the parameter from the URI (and the routing if I will never use it) and include the parameter in the JSON?
For example, instead of calling this:
myurl.com/ExampleModel/id
{
"name" : "My Name",
"anotherParameter" : "A random string"
}
You would call this:
myurl.com/ExampleModel
{
"id" : 512,
"name" : "My Name",
"anotherParameter" : "A random string"
}
First, Disclaimer: I don’t program in Ruby, so I cant answer specific questions on Ruby. But my answer is directed to your question of REST principles.
Finally, the answer:
This is a matter of how your application plays out. I will first explain a common approach used by developers. I mentioned common and is not the golden rule.
When you specify
myurl.com/ExampleModel/id. A common meaning of such a pattern is used in with GET or PUT operation. GET in the sense that I want information on some object of ID valueid. PUT in the sense that I have some request body attached with the request to be Updated to the Data Store whose Object have a ID ofid.Your second URL when you mentioned just
myurl.com/ExampleModel, A common interpretation is to show me all the records from theExampleModelcollection. This is pretty much the default behaviour used by developers who write APIs.So, does it break REST principles when you want to do things, the answer is NO. But if you hand your API to other people, the might get a bit confused over it.
So there you have it. Now it’s upto you to decide what is best suited for your application.