mirror of
https://github.com/httpie/cli.git
synced 2024-11-22 15:53:13 +01:00
6cd934d1b8
Close #684, #201
21 lines
698 B
Python
21 lines
698 B
Python
from typing import Tuple, Union
|
|
|
|
from httpie.cli.dicts import RequestDataDict, RequestFilesDict
|
|
from requests_toolbelt import MultipartEncoder
|
|
|
|
|
|
# Multipart uploads smaller than this size gets buffered (otherwise streamed).
|
|
# NOTE: Unbuffered upload requests cannot be displayed on the terminal.
|
|
UPLOAD_BUFFER = 1024 * 100
|
|
|
|
|
|
def get_multipart_data(
|
|
data: RequestDataDict,
|
|
files: RequestFilesDict
|
|
) -> Tuple[Union[MultipartEncoder, bytes], str]:
|
|
fields = list(data.items()) + list(files.items())
|
|
encoder = MultipartEncoder(fields=fields)
|
|
content_type = encoder.content_type
|
|
data = encoder.to_string() if encoder.len < UPLOAD_BUFFER else encoder
|
|
return data, content_type
|