encode_file
Encodes a file into a multipart/form-data byte sequence using a specified boundary and key. The function determines the content type based on the file's attributes or name and reads the file's content into the resulting byte list.
def encode_file(
boundary: string,
key: string,
file: file-like object
) - > list
Encodes a file into a list of byte strings formatted as a multipart/form-data MIME part. This is used to prepare file uploads for HTTP requests by generating the necessary boundary markers, headers, and binary content.
Parameters
| Name | Type | Description |
|---|---|---|
| boundary | string | The unique boundary string used to separate parts in the multipart/form-data payload. |
| key | string | The form field name associated with the file upload. |
| file | file-like object | The file object to be encoded; must support .read() and may optionally provide .name or .content_type for header generation. |
Returns
| Type | Description |
|---|---|
list | A list of byte strings representing the multipart chunks, including the boundary, Content-Disposition header, Content-Type header, and the file's raw binary data. |