2

I try to use the LogMeal API...

they give me this code

import requests
img = './banane.jpg'
api_user_token = 'xxxxxxxxxxxxxxxxxxx'
headers = {'Authorization': 'Bearer ' + api_user_token}

# Food Type Detection
url = 'https://api.logmeal.es/v2/recognition/type'
resp = requests.post(url,files={'image': open(img, 'r')}, headers=headers)
print(resp.json()) # display json response 

If I run this code I recieve this code:

Traceback (most recent call last):
  File "app.py", line 8, in <module>
    resp = requests.post(url,files={'image': open(img, 'r')}, headers=headers)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/requests/api.py", line 119, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/requests/api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/requests/sessions.py", line 528, in request
    prep = self.prepare_request(req)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/requests/sessions.py", line 456, in prepare_request
    p.prepare(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/requests/models.py", line 319, in prepare
    self.prepare_body(data, files, json)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/requests/models.py", line 507, in prepare_body
    (body, content_type) = self._encode_files(files, data)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/requests/models.py", line 159, in _encode_files
    fdata = fp.read()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

I also tryed to use open(img, 'rb')or open(img, 'rb', encoding='utf-16'/encode='utf-8')

Does somebody know how to fix this error?

  • Does this answer your question? [error UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte](https://stackoverflow.com/questions/42339876/error-unicodedecodeerror-utf-8-codec-cant-decode-byte-0xff-in-position-0-in) – MendelG Mar 11 '21 at 22:08
  • 1
    @MendelG I allready have seen this, but it don‘t worke for me – Philipp Storz Mar 12 '21 at 13:00

0 Answers0