3

I have the following code:

    multiple_files = [
    ('reference', (os.path.basename(reference), open(reference, 'rb'), 'application/pdf')),
    ('candidate', (os.path.basename(candidate), open(candidate, 'rb'), 'application/pdf'))
    ]

    payload = {'user': user, 'password': password, 'configname': configname, 'jobid': jobid}

    r = requests.post(url, files=multiple_files, data=payload, verify=False)
 

Each file that I am sending is only about 2Mb. (The code above does work successfully when the files are about 400kb each). When I execute this with the 2Mb files, I get back a 413 error (Request Entity Too Large). I need to change the code as I don't have access to the server to increase the file size.

How would I break up multiple files so that I could send it in chunks?

I've read about how to do it with 1 file, but I can't seem to find how to do it with multiple files.

I tried to change it to this based on somethings I read about streaming uploads:

with open(reference, 'rb') as a, open(candidate, 'rb') as b:
        payload = {'user': user, 
                   'password': password, 
                   'configname': configname, 
                   'jobid': jobid, 
                   'reference': (os.path.basename(reference), a, 'application/pdf'), 
                   'candidate':(os.path.basename(candidate), b, 'application/pdf')
                  }

        r = requests.post(url, data=payload, verify=False)

but that gives a new error: HTTP Status 415 - Unsupported Media Type The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.

So then I tried to add the type to the header

headers = {'content-type': 'application/pdf'}
...
r = requests.post(url, data=payload, headers=headers, verify=False)

But that returned the same 415 Error.

I also tried using another library called requests_toolbelt but that returned the 413 request entity too large as well:

from requests_toolbelt.multipart.encoder import MultipartEncoder
...
    m = MultipartEncoder(
        fields={'user': user,
                'password': password,
                'configname': configname,
                'jobid': jobid,
                'reference': (os.path.basename(reference), open(reference, 'rb'), 'application/pdf'),
                'candidate': (os.path.basename(candidate), open(candidate, 'rb'), 'application/pdf')}
    )

    r = requests.post(url, data=m, headers={'Content-Type': m.content_type}, verify=False)

If anyone can help, it would be much appreciated.

Thanks,

1 Answer 1

2

So I never found how to do this in python, I eventually had to get access to the nginx server and change the settings according to this:

https://stackoverflow.com/a/35794955/10920314

Then it worked without a problem.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.