i8abug
i8abug

Reputation: 1682

Can a single url have two different objects (single item or collect of items) be posted and still be RESTfully designed?

I have person json objects that are posted to a rest service that is responsible for creating the objects and storing them in a database. But I have two situations I need to handle.

  1. Post a single person object and have one person created
  2. Post a person array and have multiple people created

Can I use the same url...ie www.mysite.com/people/ and have the server determine whether it is a single person or a collection of people? I know I can technically do this but how should I deal with this situation and still have a RESTful design?

Upvotes: 0

Views: 98

Answers (1)

Will Hartung
Will Hartung

Reputation: 118641

Yes. "POST" is kind of a wildcard, and can "get away" with most anything. What you don't want to do is conflate this resource with the underlying resource. You don't want to POST to /people, rather something specific for the task.

So, you'll want something like /people_loader for this task that returns a explicit result of the process (/people_loader/1234), which is a resource linking to the new people you just created.

/people is your base resource, so a /people_loader result would be a collection of /people links, and perhaps some other information for those objects that weren't loaded (do to errors or other constraints).

Upvotes: 1

Related Questions