I have questions regarding the practical improvements in json / object conversion between server (api rest) and client (javascript framework). My question is is regarding the objects referenced by the main entity, should I send only IDs or complete object? As an example (simple) I have the following relationship:
{
"id":2,
"nome":"SÃO GONÇALO",
"cep":"24030103",
"estado":{
"id":1,
"nome":"RIO DE JANEIRO",
"sigla":"RJ"
}
}
This structure represents the relationship between CITY and STATE. When registering a new city (POST) should I send the complete "state" object or just its ID to the server? Example:
- Sending complete "status" object (POST):
{ "nome":"SÃO GONÇALO", "cep":"24030103", "estado":{ "id":1, "nome":"RIO DE JANEIRO", "sigla":"RJ" } }
- Sending only status ID (POST):
{ "nome":"SÃO GONÇALO", "cep":"24030103", "idEstado": 1 }
Which of these approaches should be followed?
Regarding queries (GET), should I return to the full state reference or just your ID? The return only ID approach has the "problem" of needing a new query (javascript) to be able to display the state name to the user.
I believe that just passing the ID on the POST is the best, it does not traffic unnecessary data. If it is best to follow the ID line, what best practice is to convert these IDs on the server to objects (do I need the complete object on the server to validate business before it persists)?
This was a simple example, I have templates in my business where the number of references is quite large. I work with java on the server and angular on the frontend.