Kieran
Kieran

Reputation: 4015

Correct REST API URL format for related objects

I'm designing a REST API where, amongst others, there are two objects.

For each Journey there are many Reports enroute, and each Report has exactly one associated Journey.

A user might create a Journey using the API as follows...

POST /journey/

Then retrieve the details...

GET /journey/1226/

The first question is, if a user wanted to post an Report to their Journey, which is the 'correct' URL structure that the API should impose? This seems intuitive to me...

POST /journey/1226/report/

...or is this the case...

POST /report/

...whereby in the latter, the Journey ID is passed in the request body somewhere?

The second question is, how might one go about implementing the first case in a tool such as the Django REST framework?

Thanks!

Upvotes: 1

Views: 199

Answers (1)

Robert Bräutigam
Robert Bräutigam

Reputation: 7744

The URL/URI structure is almost completely irrelevant. It is nice to be able to read it, or easily change or even guess it, but that is it. There is no "requirement" official or unwritten how they should look like.

The point is however, that you supply the URIs to your clients in your responses. Each GET will get you a representation that contains links to the next "states" that your client can reach. This means the server has full control over URI structure, the client usually has to only know the "start" or "homepage" URI, and that's it.

Here is an article which discusses this question, has some good points: http://www.ben-morris.com/hackable-uris-may-look-nice-but-they-dont-have-much-to-do-with-rest-and-hateoas/

Pass for the second question :) I didn't use that particular framework.

Upvotes: 1

Related Questions