0

I have a controller endpoint and I need help with the naming convention.

Basically, the endpoint is supposed to find a customer by their Id/Email (which is sent in the url of the request). Thus I need to have two endpoints, one for users/{id} and one for users/{email}.

The issue is, the above wouldn't work as the routes are exactly the same.

What I have done is I have replaces the users/{email} route with:

users/get-by-email/{email} and kept the get by id route as: users/{id}

Thanks

1 Answer 1

2

It seems you are creating some kind of REST API for interacting with stored data. In REST, the information you are handling is called a resource. You have users resource and emails resource. My suggestion would be that you could have a users/{id} endpoint and emails/{email} endpoint, which would return the necessary information. Further, you could also have users/{id}/emails, which would return the email addresses of a specific user etc. The purpose of naming the endpoints wisely is to aim for clarity and self-explanation.

The most in-depth discussion: Fielding, R. Representational State Transfer (REST)

6
  • 1
    But emails is a property of the users object, so is id, no? If that was the case you would have: id/{id} and email/{email}
    – Amir Ali
    Commented Jul 24, 2020 at 11:48
  • @AmirAli An email can be an object too, containing values like username, id, address... etc. In terms of naming convention, the endpoint would be more like in my answer than get-by-email type of style.
    – heilala
    Commented Jul 24, 2020 at 11:52
  • 1
    I agree with you. I've decided to change the endpoint to: user/{id} user/email/{email}
    – Amir Ali
    Commented Jul 24, 2020 at 11:54
  • @AmirAli That sounds reasonable as well. As I said, aim for clarity and consistency as the endpoint name might be difficult to change later on. Cheers!
    – heilala
    Commented Jul 24, 2020 at 11:57
  • Perfect, thank you so much. I've marked your answer as the correct and upvoted. =) Have a nice day
    – Amir Ali
    Commented Jul 24, 2020 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.