I have been toying a bit with this issue and while I fully support the "separate resources" ideas I still think it could be interesting to find some kind of standard way of posting JSON + files in one request.
As I see it we can either use multipart/form-data with both files and JSON - or application/json with files encoded in base64 and stored as strings. But I really don't think files-as-json-strings is any good idea for files of any realistic size. So multipart it is ...
Here is my approach to a multipart/json encoding:
1) Fact: multipart/form-data can contain an arbitrary number of parts/files/data, each of which can have its own content-type.
2) Have one part with application/json for the JSON encoded meta-data. Name it something arbitrarily.
3) Add as many files as required to the main multipart/form-data payload. Name each of the files in any way the client want. Allow as many files as the client want - or use a fixed number of files defined by the API. Just make sure server and client agrees.
4) Whenever the JSON data needs to refer to a file, it simply uses the name of the file-part in the multipart/form-data payload - maybe prefixed with a hashtag or similar.
Example - let us assume we want to post a job application with attached CV (PDF), a short introduction (PDF), a photo of the applicant (JPEG) and a endorsement from someone (PDF):
Primary multipart/form-data payload:
meta [application/json]: ... JSON meta data ...
intro [application/pdf]: ... binary PDF data ...
cv [application/pdf]: ... binary PDF data ...
other-1 [image/jpeg]: ... binary JPEG data ...
other-2 [application/pdf]: ... binary PDF data.
Meta data JSON:
{
applicant:
{
name: "Jason Harrison",
born: "1965-10-24",
... and more ...
},
introduction:
{
attachment: "#intro"
},
cv:
{
attachment: "#cv"
},
additional-files:
[
{
title: "Photo of me",
attachment: "#other-1"
},
{
title: "Endorsement from Tina Mülowitch",
attachment: "#other-2"
}
]
}
The server can then look for the first (only) part of type "application/json" and use that as the meta data - and then grab the attachment names in the JSON and use those to refer to the multipart/form-data files.
I think this is basically what is done with an e-mail containing attachments. You could probably just post a classic e-mail encoded bunch of data and use standard libraries to extract the information :-)
/Jørn