J Marlow
J Marlow

Reputation: 827

Naming convention for REST endpoint where ID is not provided by request

I've got a small problem where I can't seem to work out what to name a particular API endpoint.

The resource structure looks like this:

students/{student-id}/exams

Using a POST request, I want to add a new exam to a specific student. However, the student's ID, which we are using to query the students collection, is NOT provided in the request. Instead, the API logic retrieves the student's ID based on the current user session.

I thought about naming the endpoint this:

POST students/current-session/exams

But that doesn't seem right to me.

So what would you suggest as a good, RESTful name for this endpoint? I can't use query parameters as this is a POST.

Upvotes: 0

Views: 654

Answers (1)

VoiceOfUnreason
VoiceOfUnreason

Reputation: 57249

REST doesn't care about the spelling of identifiers. You can use anything you like.

There are a lot of possible candidates to consider, they are all fine.

There's no particular reason that the identifier used for this use case needs to be aligned with the students hierarchy -- if it makes your implementation simpler, fine, but there's no particular advantage for consumers.

/my/exams
/students/current/exams
/777BC64D-AE19-4544-9757-E305188E9237
/777BC64D-AE19-4544-9757-E305188E9237/exams

These are all fine. In a lot of ways, choosing a spelling for a resource identifier is like choosing a spelling for a variable name -- it only really matters to people who are reading it.

Upvotes: 1

Related Questions