Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add notes on "filename" form-data field #1704

Merged
merged 7 commits into from Jun 27, 2022
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions docs/requests.md
Expand Up @@ -112,7 +112,15 @@ state with `disconnected = await request.is_disconnected()`.

#### Request Files

Request files are normally sent as multipart form data (`multipart/form-data`).
Request files are normally sent as multipart form data (`multipart/form-data`).
Kludex marked this conversation as resolved.
Show resolved Hide resolved

!!! note
As settled in [RFC-7578: 4.2](https://www.ietf.org/rfc/rfc7578.txt), form-data content part that contains file
assumed to have `name` and `filename` fields in `Content-Disposition` header: `Content-Disposition: form-
data; name="user"; filename="somefile"`. Though `filename` field is optional according to RFC-7578, it helps
Starlette to differentiate which data should be treated as file. If `filename` field was supplied, `UploadFile`
object will be created to access underlying file, otherwise form-data part will be parsed and available as a raw
string.

When you call `await request.form()` you receive a `starlette.datastructures.FormData` which is an immutable
multidict, containing both file uploads and text input. File upload items are represented as instances of `starlette.datastructures.UploadFile`.
Expand All @@ -138,7 +146,7 @@ For example, you can get the file name and the contents with:

```python
form = await request.form()
filename = form["upload_file"].filename
filename = form["upload_file"].filename # or form.get("upload_file")
adriangb marked this conversation as resolved.
Show resolved Hide resolved
contents = await form["upload_file"].read()
```

Expand Down