Home > Software design >  I don't know how to decide path of API on REST API in updating user information situation
I don't know how to decide path of API on REST API in updating user information situation

Time:04-26

PATCH /api/users/:id/email => user update email
PATCH /api/users/:id/nickname => user update nickname

As above, I designed path of API based on Rest API.
but, I have a question
I am using jwt
jwt_token has id information of user
i think, better way is to use id of user in jwt_token instead of adding id of user to parameter.
but, I have a another question

PATCH /api/email => user update email
PATCH /api/nickname => user update nickname

if i use id of user in jwt_token, API path will change like a example above.
I think, it`s strange.
because, According to Rest API, use collection.(ex. users, books and so on..)
What is the right thing to do?
thank you for reading

CodePudding user response:

jwt_token is for authorization. Routes are supposed to be specific to what they are doing. The first example

PATCH /api/users/:id/email => user update email
PATCH /api/users/:id/nickname => user update nickname

is more clear as to what you are doing, it's more reusable, less ambiguous, it's obvious you are editing the user email and username, whereas

PATCH /api/email => user update email
PATCH /api/nickname => user update nickname

is not so obvious. I would go with the first over the second any day.

  • Related