User User -3 years ago 129
reST (reStructuredText) Question

Is it good/bad design to join composite key with underscore in url?

I'm looking for the best practices on RESTful API design for the following use case:

Table1 Table2
Id1 Id1
Id2 Id2
Id3 Id3
Name Name
Table1Id1(FK to Table1)
Table1Id1(FK to Table1)
Table1Id1(FK to Table1)


Suppose i have endpoints like below for Table1:

/root/table1 (to get list of records)
/root/table2 (to get single record by primary key)


Now here my question is which would be the best way from below two to represent composite key in second url :

/root/e1/Id1/Id2/Id3

or

/root/e1?Id1=1&Id2=2&Id3=3


Suppose i have endpoints like below for Table2:

/root/table1/Table1Id1_Table1Id2_Table1Id1/table2 (to get list of records for table2 by table1).


Now here is my question that is above url valid and appropriate in case of composite key?

Any advice on a good pattern to follow for this use case would be greatly appreciated.

Answer Source

Any advice on a good pattern to follow for this use case would be greatly appreciated.

Don't couple your resource identifiers to your (current) database schema; that violates encapsulation.

I'm looking for the best practices on RESTful API design for the following use case

REST really doesn't care. As far as REST is concerned, the URI is opaque; any information encoded into it is done at the server's discretion and for its own use.

The relevant concerns are RFC 3986, and your local design conventions.

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

Path elements are supposed to be for hierarchical data -- think about the way that relative URIs resolve.

Based on your description here, I wouldn't think that the foreign keys have a natural hierarchy to them; certainly not in the general case. So using the non hierarchical part of the URI (the query) might make more sense.

Another possibility to consider would be matrix parameters; you can combine the foreign keys into a single path segment, thereby avoiding any suggestion of hierarchy among them.

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