Search code examples
pythonpython-requestsmultipartform-data

Python multipart upload not taking content-type


I am tying to use python requests framework for multipart upload,follwoing is my code:

import requests


def myfc()
{
url = "https://myurl"

payload={}
files=[
  ('request',('test.yaml',open('/Users/test/test.yaml'))
]
headers = {
  'Content-Type': 'multipart/form-data',
  'Authorization': 'test'
}

response = requests.request("POST", url, headers=headers, data=payload, files=files,verify=False)

print(response.text)
}
print(' 1')
t1 = threading.Thread(target=myfc, args=())
print(' 2')
t2 = threading.Thread(target=myfc, args=())
print(' 3')
t3 = threading.Thread(target=myfc, args=())
t1.start()

If i remove 'Content-Type': 'multipart/form-data',from headers and keep only Authorization then it works fine, but If i keep 'Content-Type': 'multipart/form-data',then it give 404 response.Why so?


Solution

  • Try the following:

    import requests
    
    def myfc():
        url = "https://myurl"
    
        payload={}
    
        files= {
            'test.yaml' : open('/Users/test/test.yaml', 'r')
        }
        headers = {
          'Content-Type': 'multipart/form-data',
          'Authorization': 'test'
        }
    
        with requests.request("POST", url, headers=headers, data=payload, files=files,verify=False) as response:
            response.raise_for_status()
            print(response.text)
    
    print(' 1')
    t1 = threading.Thread(target=myfc, args=())
    print(' 2')
    t2 = threading.Thread(target=myfc, args=())
    print(' 3')
    t3 = threading.Thread(target=myfc, args=())
    t1.start()