Sandeep Lade
Sandeep Lade

Reputation: 1943

File upload using python requests post

I need to upload a tar.gz file using python requests. When i try to check the payload in browser network tab i could see payload as below

------WebKitFormBoundary3sUg1Lq9ivT71NNI
Content-Disposition: form-data; name="file"; filename="cop-pem-cert.tar.gz"
Content-Type: application/gzip


------WebKitFormBoundary3sUg1Lq9ivT71NNI--

Any thoughts what should be the payload and headers to be used ? Tried this but getting 400

import requests
url='https://test.athena.com/data-migration/upload'             
headers={'referer': 'https://test.athena.com', 'cookie': 'i18next=; session=3c97e9d692670f6a_621662a4.JQXBkMr0-bDZiU2G4dZpoNP8m4A; csrftoken=##39d1b4f0a4b32898623f82c0033f1ebaa04ebd4d; hpeuck_cktst=','x-csrf-token':'##39d1b4f0a4b32898623f82c0033f1ebaa04ebd4d','X-Requested-With': 'XMLHttpRequest','Content-Type': 'application/gzip'}
a=requests.post(url,headers=headers,files={'file': ('cop-pem-cert.tar.gz', open('cop-pem-cert.tar.gz', 'rb'))})

Upvotes: 0

Views: 3281

Answers (1)

Sandeep Lade
Sandeep Lade

Reputation: 1943

In the headers we should not pass 'Content-Type': 'application/gzip' while doing post . After removing that it worked

Working code

>>> headers
{'referer': 'https://dummy.test.com', 'cookie': 'i18next=; session=17158f23bdd99dd1_62179b51.a2uP2cSYO_1ASd4fpfKuQaa2jtU; csrftoken=##1440ee62328990eadd4bb9d1bdf1f15bd6a973cb; hpeuck_cktst=', 'x-csrf-token': '##1440ee62328990eadd4bb9d1bdf1f15bd6a973cb', 'X-Requested-With': 'XMLHttpRequest'}
>>> files
{'file': ('cop-pem-cert.tar.gz', <_io.BufferedReader name='cop-pem-cert.tar.gz'>, 'application/gzip')}
>>> url
'https://dummy.test.com/data-migration/upload'
>>> a=requests.post(url,headers=headers,files=files)
>>> a=requests.post(url,headers=headers,files=files)
>>> a
<Response [200]>

Upvotes: 1

Related Questions