I'm trying to convert the following curl (which works fine) to Python code:
curl -X POST https://api.example.com/c \
-H 'Authorization: Bearer {token}' \
-H 'content-type: multipart/form-data' \
-F 'attachment[c_id]=1111' \
-F 'attachment[file][email protected]'
I've tried two different options:
Option #1:
import requests
headers = {
'Authorization': 'Bearer {token}',
'content-type': 'multipart/form-data',
}
files = {
'attachment[c_id]': (None, '1111'),
'attachment[file]': ('file.png', open('file.png', 'rb')),
}
response = requests.post('https://api.example.com/c',
headers=headers, files=files)
Option #2:
import requests
from requests_toolbelt.multipart.encoder import MultipartEncoder
headers = {
'Authorization': 'Bearer {token}',
'content-type': 'multipart/form-data',
}
multipart_data = MultipartEncoder(
fields=(
('attachment[file]', open('file.png', 'rb')),
('attachment[c_id]', '1111')
))
response = requests.post('https://api.example.com/c',
headers=headers, data=multipart_data)
Both options failed with the following error:
requests.exceptions.ConnectionError: ('Connection aborted.', BrokenPipeError(32, 'Broken pipe'))
So, it means that Python code works in a different way because curl works just fine.
I've tried https://curl.trillworks.com/ - it didn't help, unfortunately. How can I do the same on Python?