Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Just because you're not wrapping the entire request body in JSON, doesn't meant it's not RESTful to use <code>multipart/form-data</code> to post both the JSON and the file (or multiple files) in a single request:</p> <pre><code>curl -F "metadata=&lt;metadata.json" -F "file=@my-file.tar.gz" http://example.com/add-file </code></pre> <p><strong>on the server side</strong> (using Python as the programming lingua franca here):</p> <pre><code>class AddFileResource(Resource): def render_POST(self, request): metadata = json.loads(request.args['metadata'][0]) file_body = request.args['file'][0] ... </code></pre> <p><strong>to upload multiple files, it's possible to either use separate "form fields" for each:</strong></p> <pre><code>curl -F "metadata=&lt;metadata.json" -F "file1=@some-file.tar.gz" -F "file2=@some-other-file.tar.gz" http://example.com/add-file </code></pre> <p>...in which case the server code will have <code>request.args['file1'][0]</code> and <code>request.args['file2'][0]</code></p> <p><strong>or reuse the same one for many:</strong></p> <pre><code>curl -F "metadata=&lt;metadata.json" -F "files=@some-file.tar.gz" -F "files=@some-other-file.tar.gz" http://example.com/add-file </code></pre> <p>...in which case <code>request.args['files']</code> will simply be a list of length 2.</p> <p><strong>or actually pass multiple files into a single field in one go:</strong></p> <pre><code>curl -F "metadata=&lt;metadata.json" -F "files=@some-file.tar.gz,some-other-file.tar.gz" http://example.com/add-file </code></pre> <p>...in which case <code>request.args['files']</code> 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.</p> <p>The difference between <code>@</code> and <code>&lt;</code> is that <code>@</code> causes the file to get attached as a file upload, whereas <code>&lt;</code> attaches the contents of the file as a text field.</p> <p><em>P.S.</em> Just because I'm using <code>curl</code> as a way to generate the <code>POST</code> 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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload