Reputation: 587
Am struggling to translate a working cURL command into an equivalent Python request. The cURL command is as follows:
curl -X POST \
http://localhost:3001/api/v1/document \
-H 'Content-Type: multipart/form-data' \
-F 'file=@/home/user1/test.pdf;type=application/pdf' \
-F '[email protected];type=application/json'
Am using Python 3.6 and the request
library and have been considering various approaches. One of those, which IMHO should work, but gives a Error: Multipart: Boundary not found error on the server is the following:
url = 'http://localhost:3001/api/v1/document'
headers = {'Content-Type': 'multipart/form-data'}
file = "/home/user1/test.pdf"
files = {
'file': (file, open(file, 'rb'),'type=application/pdf'),
'config': ('config.json',open('config.json','rb'),'type=application/json')
}
r = requests.post(url, files=files, headers=headers )
I have looked at various online "translaters", including https://curl.trillworks.com/ but none of them give a working answer. Also tried a number of variations, which I found in other posts, such as removing the content-type
in the header, or using json.dump()
instead of the open()
function, but still, no success.
Any help would be greatly appreciated!
Upvotes: 0
Views: 1082
Reputation: 587
The solution from Bertrand Martel did the trick in the end:
headers
'type='
from both, the pdf and json files. The working solution looks thus as follows:
url = 'http://localhost:3001/api/v1/document'
file = "/home/user1/test.pdf"
files = {
'file': (file, open(file, 'rb'),'application/pdf'),
'config': ('config.json',open('config.json','rb'),'application/json')
}
r = requests.post(url, files=files)
Upvotes: 1