P
P
Philip2015-09-07 14:43:44
RESTful API
Philip, 2015-09-07 14:43:44

How to adequately add a resource group with a single request to the REST API?

Let's assume that our data storage is arranged in the following way:
0ff5d92def3b454e94bbcf753faee85c.png
There was a need to implement a REST API to get/change data in the system.
It is logical to assume the following structure:
GET /users/ - getting a list of users ( ?expand=phones - phones associated with users will be received )
GET /users/{id} - getting data about a specific user ( ?expand=phones - phones associated with a given user )
POST /users/ - adding a user
PUT /users/{id} - editing user data
Same with phones.
To implement communication, we will do the same for user_phone.
And now the question itself:
The REST API involves adding only one resource using the POST method, but what if I need to bind 100 phones to a user? 100 requests in this case is somehow too much, obviously this problem needs to be solved in a different way.
Possible options:
1) Bypassing the recommendations, send an array with these resources to POST /user-phones/, for example: [{"id_user":1,"id_phone":1},{"id_user":1,"id_phone": 2}]. But then it is not clear what response and what statuses to return, how to mark errors, etc.
2) When editing the PUT /user/{id} resource, pass some phones value that will contain an array consisting of phone ids, for example: {"name ":"Alyosha","phones":[1,2,3,4,5]}.
How to do better? Are there adequate solutions to this problem, or should one of these two options be used?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question