Search code examples
restarchitectureroutesrestful-architecturerestful-url

REST route resource by user


I have many resources like product, bookings and etc. Need routes to get all resources by user.

For example:

{GET} /bookings/user

I think this is incorrect, because this route would return user resource. Or I am wrong?

What is the right way?


Solution

  • I think this is incorrect, because this route would return user resource. Or I am wrong?

    It returns a representation of the resource identified by /bookings/user. What resource is that? It's up to the origin server.

    doesn't care what spelling conventions you use for your identifiers. As far as REST is concerned, /6414b60b-1ecb-4e28-8887-4bfe120810e7 is a perfectly satisfactory resource identifier.

    If you want human readable identifiers, that's OK too. RFC 3986 encourages that practice:

    A URI often has to be remembered by people, and it is easier for people to remember a URI when it consists of meaningful or familiar components.

    You should also keep in mind that RFC 3986 distinguishes hierarchical and non-hierarchical signal in the URI

    The URI syntax is organized hierarchically, with components listed in order of decreasing significance from left to right. -- section 1.2.3

    The path component contains data, usually organized in hierarchical form, that, along with data in the non-hierarchical query component -- section 3.3

    The query component contains non-hierarchical data that, along with data in the path component (Section 3.3), serves to identify a resource within the scope of the URI's scheme and naming authority (if any) -- section 3.4

    In short, if you are describing information that is described in a hierarchy, then using path segments makes a lot of sense

    I don`t return users and user ids, need return all booking for authorized user.

    That sounds like you are describing a spelling like /my/bookings.

    A heuristic that may help in URI design is to consider the benefits of relative references; much as with a file system, you can use dot-segments to navigate toward the root of the namespace.

    `/my/bookings` + `../profile` --> `/my/profile`
    

    There are reasons where you might not want to support hackable URI; so you'll want to think about that constraint, and its implications, carefully.