requests / toolbelt

A toolbelt of useful classes and functions to be used with python-requests
https://toolbelt.readthedocs.org
Other
999 stars 186 forks source link

multipart/form-data file upload using PUT contains header in the uploaded file #373

Closed defrag-bambino closed 8 months ago

defrag-bambino commented 8 months ago

Hi, I am using the following code to upload a large file to my nextcloud:

encoder = MultipartEncoder(
    fields={'file': (file_name, open(file_path, 'rb'), 'application/octet-stream')}
)
def monitor_callback(monitor):
    self.upload_progresses[file_name] = round(monitor.bytes_read / monitor.len * 100, 1)
m = MultipartEncoderMonitor(encoder, monitor_callback)

try:
    response = requests.put(
        'https://nextcloud.mydomain.com/public.php/webdav/ ' + file_name,
        headers={'Content-Type': m.content_type},
        data=m,
        auth=('usr', 'pass),
    )

It does work, but the headers also end up as part of the file. Here is a text file example:

--740a02860fb54c1346426f09c584eb8c
Content-Disposition: form-data; name="file"; filename="Pipeline_2023-12-04_16-34-46.log"
Content-Type: application/octet-stream

ACTUAL FILE CONTENT .............

--740a02860fb54c136426f09c584eb8c--

Changing to a POST request does not work, as the nextcloud server responds with a "Method not allowed" error.

How can I transmit the file without having the headers end up in it? Thanks

sigmavirus24 commented 8 months ago

I don't have next cloud available but I'm looking at their discourse and someone is showing curl -T. What you're using is the equivalent of curl -F.

https://help.nextcloud.com/t/upload-file-to-shared-folder-using-wget/74752/4 makes me think multipart form-data isn't what you want