Best rest api naming convention for resources with association

Viewed 1172

Following are the rest endpoints I have. I want the user to call the 1st endpoint and for subsequent calls I want the userid to be in request. What are some of the best way to achieve this?

1. POST www.test.com/v1/users (return userid)
2. POST www.test.com/v1/services
3. PUT  www.test.com/v1/services/{serviceId}
4. DELETE www.test.com/v1/services/{serviceId}
5. GET www.test.com/v1/services?search="search string for dynamic query"
6. PATCH www.test.com/v1/services/{serviceId}/{state}
2 Answers

This depends of your domain and what you want to express. Usually the user credentials are part of the header. But if you want to make that explicit you can make it part of the url :

/v1/{userId}/services/

Usually I use the aggregation vs composition to decide it is a nested resource or not.

Further calls may have the userid(should encrypt?) returned by www.test.com/v1/users in the request headers.

If you need not expose the userId to the user, you can use any encoder and generate a hash value(A personal-access-token unique to that login session/Session ID). Everytime when subsequent requests are made, the session ID can be validated and proceed for further action.

Also, I feel the latter should be your way to go. Also, try setting session-keep alive time based on the Session ID generated.

Related