Just because you're not wrapping the entire request body in JSON, doesn't meant it's not RESTful to use multipart/form-data
to post both the JSON and the file(s) in a single request:
curl -F "metadata=<metadata.json" -F "[email protected]" http://example.com/add-file
on the server side:
class AddFileResource(Resource):
def render_POST(self, request):
metadata = json.loads(request.args['metadata'][0])
file_body = request.args['file'][0]
...
to upload multiple files, it's possible to either use separate "form fields" for each:
curl -F "metadata=<metadata.json" -F "[email protected]" -F "[email protected]" http://example.com/add-file
...in which case the server code will have request.args['file1'][0]
and request.args['file2'][0]
or reuse the same one for many:
curl -F "metadata=<metadata.json" -F "[email protected]" -F "[email protected]" http://example.com/add-file
...in which case request.args['files']
will simply be a list of length 2.
or pass multiple files through a single field:
curl -F "metadata=<metadata.json" -F "[email protected],some-other-file.tar.gz" http://example.com/add-file
...in which case request.args['files']
will be a string containing all the files, which you'll have to parse yourself — not sure how to do it, but I'm sure it's not difficult, or better just use the previous approaches.
The difference between @
and <
is that @
causes the file to get attached as a file upload, whereas <
attaches the contents of the file as a text field.
P.S. Just because I'm using curl
as a way to generate the POST
requests doesn't mean the exact same HTTP requests couldn't be sent from a programming language such as Python or using any sufficiently capable tool.