0

I have been using openKM for document management and after retrieving image from openkm using api it shows question marks rectangles.

I have already checked this question but did not help .

my python code for making api request

any help will be much appreciated

url = "http://ipaddress/aa18be7a5/hhhhhggg.png"

payload={}
headers = {'Internal-Key':"gjffhddsgsgdfgkhkhggdgsfd"}
response = requests.request("GET", url, headers=headers, data=payload)
return response.text

VLAZ
  • 26,331
  • 9
  • 49
  • 67
Naqib007
  • 3
  • 2

1 Answers1

0

You requested .PNG data, and that's what the server sent you. It all looks good.

You did this

response = requests.request("GET", url, ...)
return response.text

The request is beautiful.

But then you looked at .text, hoping for some unicode. That would make sense, on a text document. But instead you obtained a binary PNG document. Just look at the returned headers -- they will explain that its "Content-type" is "image/png".

You want response.content instead, which is uninterpreted binary bytes, suitable for writing to some "foo.png" file for display. (Remember to use "wb" as the open() mode, not just "w"!)

It's important to understand that some byte strings do not form valid utf-8, and trying to treat binary images as unicode will soon end in tears.

J_H
  • 17,926
  • 4
  • 24
  • 44
  • Thank you so much @J_H I have added `'Content-Type': 'image/png'` and `return response.content` instead now I get this numbers array sorry I am a kinda new to Python. and I don't have file path for open() function what should I pass instead of file path? – Naqib007 Dec 12 '22 at 07:59
  • I can save image using the following code but what I want is to return the image back to js response and preview it there using img tag `response = requests.request("GET", url, headers=headers, data=payload, stream=True) if response.status_code == 200: with open(get_file_path('filename.png'), 'wb') as f: response.raw.decode_content = True shutil.copyfileobj(response.raw, f)` – Naqib007 Dec 12 '22 at 08:07
  • converted arrayBuffer to base64 and now it is working thanks alot @J_H – Naqib007 Dec 12 '22 at 08:14