Carson Carson - 1 year ago 56
reST (reStructuredText) Question

How should I specify the resource database via HTTP Requests

I have a REST API that will be facilitating CRUD from multiple databases. These databases all represent the same data for different locations within the organization (IE We have 20 or so implementations of a software package and we want to read from all of the supporting databases via one API).

I was wondering what the "Best Practice" would be for facilitating what database to access resources from?

For example, right now in my request headers I have a custom "X-" header that would represent the database id. Unfortunately, this sort of thing feels a bit like a workaround.

I was thinking of a few other options:

  1. I could bake the Database Id into the URI (/:db_id/resource/...)

  2. I could modify the Accept Header like someone would with an API version

  3. I could split up the API to be one service per database

Would one of the aforementioned options be considered "better" than the others, and if not what is considered the "best" option for this sort of architecture?

I am, at the moment, using ASP.NET Web API 2.

Answer Source

These databases all represent the same data for different locations within the organization

I think this is the key to your answer - you don't want to expose internal implementation details (like database IDs etc.) outside your API - what if you consolidate? or change your internal implementation one day?

However, this sentence reveals a distinction that is meaningful to the business - the location.

So - I'd make the location part of the URI:


Then map the locationId internally to a database ID. LocationId could also be a name, or a code, or something unique that would be meaningful to the API client.

Then - if you later consolidate multiple locations to the same database or otherwise change your internal implementation, the clients don't have to change.

In addition, whoever is configuring the client applications, can do so thinking about something meaningful to the business - the location they are interested in.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download