I am trying to upload a file using Python but I am not able to represent the same code as in CURL. I have a CURL command that it's working fine with the following call:
curl -X POST
https://my_domain/workers
-H 'Cache-Control: no-cache'
-H 'Content-Type: multipart/form-data'
-H 'content-type: multipart/form-data; boundary=----0987'
-F myId=1234
-F file=@<FILE_PATH>
My Python code is like this:
headers = {
'Cache-Control': 'no-cache',
'Content-Type': 'multipart/form-data',
'content-type': 'multipart/form-data; boundary=----0987'
}
files = {
'myId': workspace_id,
'file': open(<FILE_PATH>, 'rb')
}
res = requests.post('https://my_domain/workers', headers=headers, files=files)
But when I call with Python I receive a result from the API telling that myId parameter is missing, but the same call is working in CURL. Does anyone have any idea?
Testing with https://httpbin.org/post I have the following results:
{
"args": {},
"data": "",
"files": {
"myId": "1234",
"file": "<file content>"
},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "23430",
"Content-Type": "multipart/form-data; boundary=<random number>",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.19.1"
},
"json": null,
"origin": "<IP>",
"url": "https://httpbin.org/post"
}
But this way I receive error 500 from the response. Weird because CURL command is really working as expected. I also tried removing headers dic from the call and the same problem occurred.
Thank you very much!