requests icon indicating copy to clipboard operation
requests copied to clipboard

Clarify docs for multipart file uploads

Open SyntaxColoring opened this issue 1 year ago • 3 comments

This makes some documentation changes that clarify the files argument of requests.post() et. al.

Be more precise when we say "multiple files"

The files argument can either be a dict:

files = {
    "field_1": file_1,
    "field_2": file_2,
}

Or a list of tuples:

files = [
    ("field_1": file_1),
    ("field_2": file_2),
]

A few places implied that to "upload multiple files in one request," you had to use the list-of-tuples syntax. But the dict syntax supports multiple files just fine. The added power in the list-of-tuples syntax seems to be that you can upload multiple files to the same form field. So, we now say that.

Update the reference docs

The API reference documentation did not completely describe what values were acceptable for files, and in some cases it was misleading.

  • It didn't mention the list-of-tuples syntax at all.
  • It said you could pass a "dictionary of 'name': file-like-objects". The pluralization there makes it look like you can do something like {"name": [file_1, file_2]}, but that's not correct.
  • It didn't describe what you were allowed to provide as a "fileobj". (It can be a file-like object, or str contents, or bytes contents.)
  • It said name to refer to the value that Requests uses as the form field name. This could be misconstrued as the file name. RFC 7578 and other examples within Requests call it the "field name" or "form field name."

My sources for these changes:

It's difficult to describe this API concisely because it accepts so many different input shapes, and these additions do make the rendered docs feel a bit crowded.

SyntaxColoring avatar Mar 19 '23 22:03 SyntaxColoring

The primary benefit of using list of tuples is that order is preserved which is important for some servers that expect fields in a very specific ordering which is way more than it should be

sigmavirus24 avatar Mar 20 '23 01:03 sigmavirus24

@sigmavirus24 Thanks. To be clear, do you think some of this PR needs to change to reflect that, or were you just commenting to lend context?

SyntaxColoring avatar Mar 20 '23 13:03 SyntaxColoring

Bump. Are any changes needed for this?

SyntaxColoring avatar Jun 02 '23 14:06 SyntaxColoring