0

I've read multiple best practice guides about REST routing, but none of them could answer how to work with entities which have more unique parameters then just ID.

So for example I can have ID and Code (both are unique) for entity USER.

Simple get by ID can look like:

/users/{id}

and get by Code

/users/{code}

but what if ID and CODE are both integers?

I can create route with query string filter like:

/users?code=123

But I suppose that filter route should return List of users and also there can be multiple parameters that results in return of list of users.

/users?firstName=abcd

So what's the best way to handle routes for entities like this?

Alrick
  • 79
  • 9

2 Answers2

1

Keep in mind that REST doesn't care about what spelling conventions you use for your identifiers. Either two URI are the same, in which case the client will assume they refer to the same resource, or the two URI are different, in which case the client will assume they refer to different resources.

/users/{id}
/users/{code}

In this case, it sounds like your problem is trying to distinguish which resource is intended when the integer you get is ambiguous.

Since the integer is ambiguous, you need to make the distinction in the URI elsewhere. REST doesn't particularly care how you do that, so long as the identifiers are consistent with RFC 3986.

/ebc8a29b-76a5-4f40-b37a-2d40eab9283d/{id}
/9193590d-fc47-4107-a160-e344b9cedb5c/{code}

Is a perfectly satisfactory solution as far as REST is concerned.

/usersById/{id}
/usersByCode/{code}

That's also perfectly satisfactory, and is perhaps more easily consumed by humans.

/users/id={id}
/users/code={code}

Another satisfactory example; which gives you readability and the shared /users path segment, which may be convenient if you want relative references embedded in a representation.

Matrix parameters is a commonly used term that covers a number of different ways of embedding information within a path segment; there's a reasonable hope that, if you are using a standard/3rd-party library for parsing your URI, that some or all of those conventions will be supported.

Community
  • 1
  • 1
VoiceOfUnreason
  • 52,766
  • 5
  • 49
  • 91
0

If Id and Code don't have the same values I would prefer this style. "/users/{idOrCode}". I can write my query like this: where(x => x.Id == id || x.code == idOrCode)

Sinan Bozkuş
  • 317
  • 4
  • 9