0

I have two types of resources: Query and User.

I am POSTing to /queries to create a new query. and POSTing to /users to create a new user.

Which path should I post to , to create a new Query collection and a new User collection ?

As per my understanding, we POST to a resource/collection parent , to create that resource/collection. In this case, the parent for Users and Queries would be the same, if I POST to the parent, how do I identify which collection should be created?

Can I use a factory path to generate these collections, if yes, how?

TL;DR - I use /queries and /users to create a Query and User resource respectively, what path should i use to create the Queries and Users collection?

EDIT: I am looking for advice on how this is usually achieved in REST.

7
  • 2
    You have multiple query collections, each of which has zero or more queries?
    – Eric Stein
    Commented Apr 26, 2017 at 14:12
  • no, i have a single Query collection and a single User collection
    – gaurav5430
    Commented Apr 26, 2017 at 14:30
  • the admin can send a list of queries to create the whole query collection. the admin can send a single resource to create a single query.
    – gaurav5430
    Commented Apr 26, 2017 at 14:52
  • For simplicity, both endpoints could accept an array as request body. The users may send a full array of entities and the Admins just an array of 1 element.
    – Laiv
    Commented Apr 26, 2017 at 19:50
  • @Laiv they can accept an array, i am just looking for the appropriate way in REST to do this. Also, in this case, it looks like a hack to check the array length equal to 1, everytime i need to overwrite the query collection.
    – gaurav5430
    Commented Apr 27, 2017 at 8:38

1 Answer 1

1

I am not sure what you try to achieve. You said that you still have just one collection under /queries and /users. What should be the result of sending new collection?

  1. New collection overwrites currently existing collection

    In this case new collection effectively substitutes an existing one. In that case I would say that PUT request to /queries and /users with new collection as a payload is the way to go.

  2. Elements from new collection are appended to existing entries

    In that case you can provide new endpoint for that, e.g. POST /queries/bulk with collection of queries to append.


If your concern is that underlying collection might not exist when POSTing new (single) element – that should not bother API user at all. GET /queries should be considered a list (maybe empty one), whether or not something physically exists there. Similarily, POST /queries should always work, and it's API implementation job to create underlying physical collection if one doesn't exist.

3
  • thanks for a fairly good explanation. I am trying to follow the guidelines from here restapitutorial.com/lessons/httpmethods.html. It says, POST request to parent when you want to create a resource. The whole queries collection is a resource in itself, so it wasn't clear to me if I should POST to the /queries endpoint to create Queries. my requirement is to create a new collection, similar to 1, but no previous collection exists.
    – gaurav5430
    Commented Apr 27, 2017 at 12:40
  • But a collection is not a resource per se. I don't know if I understand correctly, but I think that you are thinking of a collection here like e.g. List in Java, where one needs to create new instance of the list to put elements in it. That's a total mix of concepts. For API user, the collection of queries under /queries always exists, but can be empty.
    – Archie
    Commented Apr 27, 2017 at 13:01
  • “The key abstraction of information in REST is a resource. Any information that can be named can be a resource: a document or image, a temporal service (e.g. "today's weather in Los Angeles"), a collection of other resources, a non-virtual object (e.g. a person), and so on. In other words, any concept that might be the target of an author's hypertext reference must fit within the definition of a resource. A resource is a conceptual mapping to a set of entities, not the entity that corresponds to the mapping at any particular point in time.” - Roy Fielding
    – Laiv
    Commented Apr 27, 2017 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.