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'))})
asked Feb 24, 2022 at 10:32
Sandeep Lade
1,9432 gold badges16 silver badges25 bronze badges
-
Does this answer your question? How to upload file with python requests?anotherGatsby– anotherGatsby2022年02月24日 10:36:41 +00:00Commented Feb 24, 2022 at 10:36
-
No that didn't solve my problemSandeep Lade– Sandeep Lade2022年02月24日 11:16:07 +00:00Commented Feb 24, 2022 at 11:16
1 Answer 1
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]>
answered Feb 28, 2022 at 14:59
Sandeep Lade
1,9432 gold badges16 silver badges25 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py