title: Using Custom Headers for Response Messages; Bad Practice?
body: <p>I am working on a REST API using Java and the Spring Framework. Currently, I return a message from the server in a custom HTTP header called <code>Server-Response</code>. This is used in both cases where errors occur and successful requests are completed. <strong><em>Is using a custom HTTP header for this purpose a bad practice?</em></strong></p>
  
  <p>Why did I do this?</p>
  
  <ul>
  <li>Cases exist where I need the body for an object but require an additional string response.</li>
  <li>Java is strictly typed; if I return a <code>List&lt;Object&gt;</code>, then I cannot return an additional string.</li>
  <li>Messages from the server must be more specific than what is provided by a global exception handler.</li>
  </ul>
  
  <p>Why not to do this?</p>
  
  <ul>
  <li>Spring provides a <code>@ExceptionHandler</code> annotation to allow exceptions to be handled differently, allowing for a <code>String</code> response.</li>
  <li>Maybe headers should not contain important information such as error messages.</li>
  </ul>
  
  <blockquote>
    <p>Sample <strong>GET</strong> Request</p>
  </blockquote>
  
  <p>I can get a list of locations from the following example URL: <code>https://fakeurl.com/api/locations</code></p>
  
  <p><strong>Request Headers</strong></p>
  
  <ul>
  <li>Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,<em>/</em>;q=0.8</li>
  <li>Accept-Encoding:gzip, deflate, sdch Accept-Language:en-US,en;q=0.8</li>
  <li>Cache-Control:no-cache Connection:keep-alive</li>
  <li>Upgrade-Insecure-Requests:1</li>
  <li>User-Agent:Mozilla/5.0 (Windows NT 6.1;
  WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94
  Safari/537.36</li>
  <li>Status Code:200 OK</li>
  </ul>
  
  <p><strong>Response Headers</strong></p>
  
  <ul>
  <li>Content-Type:application/json;charset=UTF-8</li>
  <li>Date:Wed, 11 May 2016
  15:41:03 GMT</li>
  <li>Expires:Wed, 31 Dec 1969 19:00:00 EST Expires:-1</li>
  <li>Server:Apache-Coyote/1.1</li>
  <li><strong>Server Response:Successfully retrieved all locations!</strong></li>
  <li>Transfer-Encoding:chunked</li>
  <li>X-Powered-By:Servlet 2.5; JBoss-5.0/JBossWeb-2.1</li>
  </ul>
  
  <p><strong>Response Body</strong></p>
  
  <pre><code>[
      {"locId":1,"descr":"New York","activeStatus":"ACTIVE"},
      {"locId":2,"descr":"Los Angelas","activeStatus":"ACTIVE"},
      {"locId":3,"descr":"Canada","activeStatus":"ACTIVE"},
      {"locId":4,"descr":"Mexico","activeStatus":"ACTIVE"},
      {"locId":5,"descr":"Nebraska","activeStatus":"ACTIVE"},
      {"locId":6,"descr":"Texas","activeStatus":"ACTIVE"},
      {"locId":7,"descr":"Michigan","activeStatus":"ACTIVE"}
  ]
  </code></pre>