'curl to python for post request
I would like to "translate" my curl request into python using request or urllib.
I try to post a file.
My curl request is :
curl -X POST -H "Content-Type: multipart/form-data" -H "Authorization: Bearer $1" -F "data=@$2;filename=$3" --cert certificateprivate.pem --cacert MyDigiCertSHA2.crt.pem <my_url>
I tried the code below ( doesn't work , response 400)
def upload_file(token,file_path):
ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH,cafile='MyDigiCertSHA2.crt.pem')
ctx.load_cert_chain(certfile='certificateprivate.pem')
url = <my_url>
hdr = {"Content-Type": "multipart/form-data","Authorization":"Bearer "+token}
data = '{"filterList":[{}]}'
with open(file_path, 'rb') as f:
from urllib import request, parse
data = parse.urlencode( {'filename':file_path,"data": f.read()}).encode()
req = request.Request( url, data = data,headers=hdr)
resp = request.urlopen(req, context=ctx)
content = resp.read()
data = json.loads(content.decode('utf-8'))
print(data)
Solution 1:[1]
The curl command has "Content-Type: multipart/form-data", but your python code has "Content-Type": "application/json".
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Code-Apprentice |
