A
A
Alexion2018-10-16 09:24:38
RESTful API
Alexion, 2018-10-16 09:24:38

Best practice for relationships in REST API?

I reviewed a huge number of examples, but did not find a unified approach to transferring relationships to the REST API.
What is the best way to organize the transmission and reception of links?
For example, which is better {"categoryId": 1}or {"category": {"id": 1}}?
Many services accept POST in one format, and when GET is given in another, for example, when creating, a categoryId is passed, and when issuing, the expanded category: {} is already returned. I think this is generally inconvenient, since the form should be uniform.
I found the best solution in JSON:API, but it seems to me that it is redundant, and so far there is no clear understanding of transactions.
In the example from Swagger called PetStore, everything is also out of order.
Who solves these problems?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey Ukolov, 2018-10-16
@alexey-m-ukolov

I reviewed a huge number of examples, but did not find a unified approach to transferring relationships to the REST API.
It's because it doesn't exist.
For example, which is better {"categoryId": 1} or {"category": {"id": 1}}?
As you like, it's better.
I think this is generally inconvenient, since the form should be uniform.
Then don't do it, no one is forcing you.
Who solves these problems?
You forgot to specify the problem. There is nothing in the question other than "I don't like it this way".

�
âš¡ Kotobotov âš¡, 2018-10-16
@angrySCV

there are no rules, there are only general considerations for creating a convenient API
examples are taken out of context, both examples are bad.
The first example focuses on simplifying BUT then if you have only ONE field for the category (ID), then you just write -> {"category": 1} (without ID in the name)
that is, if you simplify, then simplify everything, including names .
But most likely, the category has not only an identifier, but also a name, which cannot be simplified in one field, so you still need to display the category object, with two id fields, and name,
just display an object with one nested field - this is unnecessary redundancy, it is better to replace this with the first option.
-------
about: POST in one format, and with GET they give in another.
You just have to beat him in the face for this, as this implies the need for the developer to create 2 different representations for the data structures of the same object.

I
Ivan Shumov, 2018-10-16
@inoise

Has HAL + HATEOAS

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question