title: Multipart HTTP response
body: <p>The goal is for a Node.js / <a href="https://hapijs.com/" rel="noreferrer">hapi</a> API server to respond to a browser's AJAX request with two things:</p>
  
  <ul>
  <li>A media file (e.g. an image)</li>
  <li>A JSON object with metadata about the file</li>
  </ul>
  
  <p>These are two separate items only because binary data cannot easily be stored in JSON. Otherwise, this would be a single resource. Nevertheless, it is preferable that they be sent in a single response.</p>
  
  <p>We upload these in a single request with <code>multipart/form-data</code>. In that case, browsers provide a built-in mechanism to serialize the body and most server-side frameworks know how to parse it. <strong>But how does one do the same for a response, in the opposite direction?</strong> Namely, how should a server serialize the body to transmit it to a client?</p>
  
  <p>From what I can tell, <code>multipart/mixed</code> could be a useful content type. But there is very little talk of this. Most people seem to resort to providing two separate <code>GET</code> routes, one for each piece. I dislike that because it opens you up to race conditions, amongst other things. What am I missing?</p>
  
  <p>See also my question in <a href="https://github.com/hapijs/discuss/issues/563" rel="noreferrer">hapijs/discuss#563</a>.</p>