Skip to main content

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

NameTypeDescription
boundarystringThe unique boundary string used to separate parts in the multipart/form-data payload.
keystringThe form field name associated with the file upload.
filefile-like objectThe file object to be encoded; must support .read() and may optionally provide .name or .content_type for header generation.

Returns

TypeDescription
listA list of byte strings representing the multipart chunks, including the boundary, Content-Disposition header, Content-Type header, and the file's raw binary data.