I'm having trouble making an api request using python's request lib.
I have tested the endpoint using cURL and it works, but something in my python code makes the api call fail with status 500. My guess is it's something related to how the data/files are structured. Does anyone see something obviously wrong that I'm failing to see?
I first tried with Postman, which worked, and the cURL command it outputs also works.
curl --location --request PUT 'https://myurl/endpoint'
--header 'client-id: my-client-id'
--header 'client-secret: my-client-secret'
--header 'correlation-id: my-correlation-id'
--header 'backend: service'
--form 'entity_content={"Title":"1megabytetestimage","PathOnClient":"1mbimage.jpg"};type=application/json'
--form 'VersionData=@"/path/to/1mbimage.jpg"'
--cert certificate.pem
The python code that doesn't work is:
import requests
url = 'https://myurl/endpoint'
headers = {
'client-id': 'my-client-id',
'client-secret': 'my-client-secret',
'correlation-id': 'my-correlation-id',
'backend': 'service',
}
data = {
'entity_content': '{"Title":"1megabytetestimage","PathOnClient":"1mbimage.jpg"};type=application/json'
}
files = {
'VersionData': ('1mbimage.jpg', open('/mnt/d/1mbimage.jpg', 'rb'), 'image/jpeg'),
}
response = requests.put(url, headers=headers, files=files, data=data, cert='certificate.pem')
I've tried a large amount if iterations with small changes here and there and nothing seems to work. Unfortunately I cannot provide the actual url, client-id and secret.
Edit:
The response I get is:
--ec2f2bf4ebd9869d1d6a6a2d7f3b1ab7
Content-Disposition: form-data; name="entity_content"
{"Title":"1megabytetestimage","PathOnClient":"1mbimage.jpg"};type=application/json
--ec2f2bf4ebd9869d1d6a6a2d7f3b1ab7
Content-Disposition: form-data; name="VersionData"; filename="1mbimage.jpg"
Content-Type: image/jpeg
<Image Bytes>
--ec2f2bf4ebd9869d1d6a6a2d7f3b1ab7--
Image Bytes seems to be the whole image file in bytes format, which is unreadable.
files
to an array, like they show in the documentation? Also, does the Python code produce any error message? If yes, you could add it into your question.data
dict? I think requests takes care of handling that.--http1.1
option added to the cURL command, and see if that still works correctly?