I have a Spring boot API service , other websites and mobile apps call this API for data and images. Images are stored in file system and file path in database. I am looking for best practice to return image URLs in API response.
Sample DB Record
+-----+-------+-----------------+
| Id | Name | profile_img_url |
+-----+-------+-----------------+
| 1 | John | /image/john.jpg |
| 2 | Jane | /image/jane.jpg |
| 3 | Doe | /image/doe.jpg |
+-----+-------+-----------------+
For example I have a member API.
https://example.com/get/member/id
This API will return JSON , which has member image URL
Should I return profile_img_url as absolute URL or relative URL
Here are my thoughts
Response with Relative URL
{
"name" : "John",
"profile_img_url": "/image/john.jpg",
}
- In API code , No need to worry about base URL(domain name), the calling website or mobile app need to take care of the base URL.
- In API I just read and send the profile img url and need not spend time in concatenating domain name and file name to make absolute URL, so response should be fast.
Response with Absolute URL
{
"name" : "John",
"profile_img_url": "https://example.com/image/john.jpg",
}
In my API , I need to find the host name and concatenate the file path for every request leading to unnecessary load on API server.
If I am returning a list of member, I need to fetch from DB and loop through the list and make absolute URL. this can make the API response slower.
Also I need to ensure, my API server behind proxy or load balance, can detect the domain/ host name and use it for concatenating
Calling website or mobile app, need not worry about base URL/domain name
I dont have to educate the mobile team or client app development team on how to get the profile image
In future, if i decide to server my images from different domain or sub domain, I dont have to inform other teams
If it is a simple website (front end+back end together) , I think relative URL would be sufficient for a website unless you are running high traffic website which needs very high performance optimization.
In my case I run only back end API. Front End will be other applications / mobile apps of our company or our partners
you can also read this page, though it is not directly related to my question, but it talks about issue related to URLs in decoupled system
so what is the best practice , absolute or relative URL in a API response.