I'm designing an API for a website where users can share images and all other users can see these images. The current idea is to have a path
/user/name/images/xyz to GET a user's image and then a path images/abc to GET any public image "abc." I'm worried that there is a design flaw if I'm separating image retrieval across multiple endpoints. What's the most proper way to design this?
-
Where does the "xyz" or "abc" come from? Is that an id that will be generated by your system or is it something that is controlled by a user?Bart van Ingen Schenau– Bart van Ingen Schenau2020-02-10 17:51:21 +00:00Commented Feb 10, 2020 at 17:51
-
Sorry that's meant to be an id, possibly a hashcode or something uniquely identifying of the image.Bennet Leff– Bennet Leff2020-02-10 19:48:22 +00:00Commented Feb 10, 2020 at 19:48
1 Answer
The URL /users/NAME/images/XYZ infers that image XYZ is a subresource of user NAME. This also means that /users/bob/images/XYZ and /users/edna/images/XYZ are different images even though they share the same name (XYZ). If no such semantic relationship exists, or no two images should have the same name, then /images/XYZ is preferable.
Now imagine what happens if the client requests /users/NAME/images. Should they get a list of images filtered by that user? What happens if the client requests /images?
The answers to these questions can help drive your URL structure.
-
I think images should not strictly be a subresource of
user/NAME. I guess that I'm asking whether it's okay design to have a/images/XYZfor any image and/user/NAME/images/to get the collection of user images? Or does this overlapping "resource" indicate a flaw in design.Bennet Leff– Bennet Leff2020-02-11 13:39:31 +00:00Commented Feb 11, 2020 at 13:39