title: Static Assets URL as absolute path or relative path in API Response
body: <p>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.</p>
  
  <p><strong>Sample DB Record</strong></p>
  
  <pre><code> +-----+-------+-----------------+
  | Id  | Name  | profile_img_url |
  +-----+-------+-----------------+
  |  1  | John  | /image/john.jpg |
  |  2  | Jane  | /image/jane.jpg |
  |  3  | Doe   | /image/doe.jpg  |
  +-----+-------+-----------------+
  </code></pre>
  
  <p>For example I have a member API.  </p>
  
  <pre><code>https://example.com/get/member/id
  </code></pre>
  
  <p>This API will return JSON , which has member image URL</p>
  
  <p>Should I return profile_img_url as absolute URL or relative URL</p>
  
  <p>Here are my thoughts</p>
  
  <p><strong>Response with Relative URL</strong></p>
  
  <pre><code>{
  "name" : "John",
  "profile_img_url": "/image/john.jpg",
  }
  </code></pre>
  
  <ul>
  <li>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.</li>
  <li>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.</li>
  </ul>
  
  <p><strong>Response with Absolute URL</strong></p>
  
  <pre><code>{
  "name" : "John",
  "profile_img_url": "https://example.com/image/john.jpg",
  }
  </code></pre>
  
  <ul>
  <li><p>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.</p></li>
  <li><p>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.</p></li>
  <li><p>Also I need to ensure, my API server behind proxy or load balance,
  can detect the domain/ host name and use it for concatenating</p></li>
  <li><p>Calling website or mobile app, need not worry about base URL/domain
  name</p></li>
  <li><p>I dont have to educate the mobile team or client app development team
  on how to get the profile image</p></li>
  <li><p>In future, if i  decide to server my images from different domain or
  sub domain,  I dont have to inform  other teams</p></li>
  </ul>
  
  <p>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.</p>
  
  <p>In my case I run only back end API.  Front End will be  other applications / mobile apps  of our company or our partners </p>
  
  <p>you can also read <a href="https://www.freecodecamp.org/news/never-use-an-absolute-path-for-your-apis-again-9ee9199563be/" rel="noreferrer">this</a> page, though it is not directly related to my question, but it talks about issue related to URLs in  decoupled system</p>
  
  <p>so what is the best practice , absolute or relative URL  in a API response.</p>