Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rest_client.download_ota_package : UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte #62

Open
ysimonx opened this issue Jul 5, 2022 · 2 comments

Comments

@ysimonx
Copy link

ysimonx commented Jul 5, 2022

While trying to download a gz file as OTA package thanks to thingsboard-python-rest-client
I have this error

Traceback (most recent call last):
  File "ota.py", line 31, in <module>
    downloads=rest_client.download_ota_package(str(first_package.id.id))
  File "/home/ysimonx/Developpement/thingsboard_ota_py/env/lib/python3.8/site-packages/tb_rest_client/rest_client_ce.py", line 589, in download_ota_package
    return self.ota_package_controller.download_ota_package_using_get(ota_package_id=ota_package_id)
  File "/home/ysimonx/Developpement/thingsboard_ota_py/env/lib/python3.8/site-packages/tb_rest_client/api/api_ce/ota_package_controller_api.py", line 142, in download_ota_package_using_get
    (data) = self.download_ota_package_using_get_with_http_info(ota_package_id, **kwargs)  # noqa: E501
  File "/home/ysimonx/Developpement/thingsboard_ota_py/env/lib/python3.8/site-packages/tb_rest_client/api/api_ce/ota_package_controller_api.py", line 201, in download_ota_package_using_get_with_http_info
    return self.api_client.call_api(
  File "/home/ysimonx/Developpement/thingsboard_ota_py/env/lib/python3.8/site-packages/tb_rest_client/api_client.py", line 409, in call_api
    return self.__call_api(resource_path, method,
  File "/home/ysimonx/Developpement/thingsboard_ota_py/env/lib/python3.8/site-packages/tb_rest_client/api_client.py", line 193, in __call_api
    response_data = self.request(
  File "/home/ysimonx/Developpement/thingsboard_ota_py/env/lib/python3.8/site-packages/tb_rest_client/api_client.py", line 431, in request
    return self.rest_client.GET(url,
  File "/home/ysimonx/Developpement/thingsboard_ota_py/env/lib/python3.8/site-packages/tb_rest_client/rest.py", line 237, in GET
    return self.request("GET", url,
  File "/home/ysimonx/Developpement/thingsboard_ota_py/env/lib/python3.8/site-packages/tb_rest_client/rest.py", line 225, in request
    r.data = r.data.decode('utf8')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte


The firmware package is a gz file

and of course, this file can not be considered as a "utf-8" string,

It should not be decoded in line 225 of rest.py

` if _preload_content:
r = RESTResponse(r)

        # In the python 3, the response.data is bytes.
        # we need to decode it to string.
        if six.PY3:
            r.data = r.data.decode('utf8')

`

How to reproduce it :



with RestClientCE(base_url=url) as rest_client:
    try:
       
        rest_client.login(username=username, password=password)

       
        packs = rest_client.get_ota_packages_v1(100,0,None, None, None)

        package = packs.data[0]
        
        downloads=rest_client.download_ota_package(package.id.id)

        print(downloads)
        
    except ApiException as e:
        logging.exception(e)


@ysimonx
Copy link
Author

ysimonx commented Jul 6, 2022

here is the workaround I made

  rest_client.login(username=username, password=password)
        
        token = rest_client.api_client.configuration.api_key["X-Authorization"]

        packs = rest_client.get_ota_packages_v1(100,0,None, None, None)
      

        first_package = packs.data[0]
      
        url = "https://xxxx.com"+ "/api/otaPackage/{otaPackageId}/download".replace("{otaPackageId}",first_package.id.id )
       
        with open(first_package.file_name, "wb") as f:
            r = requests.get(url,  headers={"X-Authorization":"Bearer " + token } )
            f.write(r.content)

@imbeacon
Copy link
Member

imbeacon commented Jul 6, 2022

Hi @ysimonx ,

Thank you for your message, we will check and fix the rest client.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants