我需要使用python请求上传一个tar.gz文件。当我尝试检查浏览器网络选项卡中的有效载荷时,我可以看到如下所示的有效载荷
------WebKitFormBoundary3sUg1Lq9ivT71NNI
Content-Disposition: form-data; name="file"; filename="cop-pem-cert.tar.gz"
Content-Type: application/gzip
------WebKitFormBoundary3sUg1Lq9ivT71NNI--有什么想法,应该使用什么有效载荷和标题?试过了,但得到了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'))})发布于 2022-02-28 14:59:05
在标题中,我们不应该在发布时传递'Content-Type': 'application/gzip'。移除之后,它起作用了
工作代码
>>> 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]>https://stackoverflow.com/questions/71250585
复制相似问题