Outils pour utilisateurs

Outils du site


pele_mele:stack_exchange:stackoverflow-29661578

How to handle a POST method that generate multiple resource with REST?

I am building my first REST API. I could do most of my queries without any problem but now I encountered a use case I don't know how to solve.

Here is the use case. I submit a dataset to the API, the dataset is then stored in database (this part works as intended). When stored in the database, it creates different resources due to business rules. So now I don't know how to inform the user what is the location of the newly created resource since I could have more than one.

I read this Can the Location header be used for multiple resource locations in a 201 Created response? which tells me that only one location header is allowed. Should I rethink my POST method? Should I use a different way to acknowledge the user where are the resources?

Yes, the Location header requires a single identifier. It's intended for one resource you should follow in order to complete the request according to some predefined semantics.

You can use the Link header instead. Then you can have multiple URIs. Check the RFC 5988 here for a few examples and don't forget to document it properly.

As an alternative, keep in mind that the semantics of the POST method are determined by you, so there's nothing wrong with returning the list of links in the response payload, as long as the resource format allows it in some way and it's documented.

pele_mele/stack_exchange/stackoverflow-29661578.txt · Dernière modification : 2020/11/18 06:23 de alexis