Question
Multipart HTTP response
The goal is for a Node.js / hapi API server to respond to a browser's AJAX request with two things:
- A media file (e.g. an image)
- A JSON object with metadata about the file
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.
We upload these in a single request with multipart/form-data
. In that case, browsers provide a built-in mechanism to serialize the body and most server-side frameworks know how to parse it. But how does one do the same for a response, in the opposite direction? Namely, how should a server serialize the body to transmit it to a client?
From what I can tell, multipart/mixed
could be a useful content type. But there is very little talk of this. Most people seem to resort to providing two separate GET
routes, one for each piece. I dislike that because it opens you up to race conditions, amongst other things. What am I missing?
See also my question in hapijs/discuss#563.