REST API: Send image to image item (Python): Error 415

Hi

I’m trying to send an image from Python to openHAB using the REST API and requests.post, but I’m always getting 415: ‘Unsupported Media Type’

I’ve seen a few posts and some code fragments here about this topic but nothing worked, so I wanted to consolidate. Here’s what I’m doing:

import requests
from io import BytesIO
import base64

...
plt.savefig("/tmp/openHAB_WW.jpeg", format="jpeg")

with open("/tmp/openHAB_WW.jpeg", "rb") as image_file:
  base64_utf8_str = base64.b64encode(image_file.read())
        
data = f'data:image/jpeg;base64,{base64_utf8_str}'

headers = { "Content-Type": "image/jpeg" }

response = requests.post(url, headers=headers, data=data)

What’s wrong with that code? BTW: save as png and using image/png doesn’t work either.

I also had a look into API Explorer, there the request body has to be text/plain (drop down with only this entry).

You data looks correct. Does it work if you not set a header - normally it should not be required? Do you pass auth?

Without headers, I get the same 415 response.

No auth, and setting “ordinary” items works well.

Edit: adding auth=(, ) (values as I use to login to the web UI) to requests.post doesn’t change anything.

Can you try “text/plain” as content type?
I looked at the HABApp code and there I don’t have to manually set a content type.

If all else fails you can use HABApp to run your python code and send the image there :wink:

Then I get 400 ‘Bad request’

OK, “solved” the problem by sending the image via MQTT…