0

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.

7
  • 1
    Hi, could you try change 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.
    – marek
    Commented Feb 27, 2024 at 13:18
  • Have you tried without the ";type=application/json" part in the value-part of the data dict? I think requests takes care of handling that.
    – 9769953
    Commented Feb 27, 2024 at 13:18
  • @marek I have indeed tried that and I still get status 500.
    – Diogo Vala
    Commented Feb 27, 2024 at 15:22
  • @9769953 I also tried that to no avail.
    – Diogo Vala
    Commented Feb 27, 2024 at 15:22
  • Just to check: could you try with the --http1.1 option added to the cURL command, and see if that still works correctly?
    – 9769953
    Commented Feb 27, 2024 at 16:15

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.