Skip to content

Conversation

@jgoclawski
Copy link
Member

@jgoclawski jgoclawski commented Mar 6, 2025

The API does not assume any particular encoding, so there are no sane defaults. Strings can be encoded with various encodings and are transferred as bytes. For one user latin-1 will be the best one to use to decode those bytes, for another - windows-1250 will work better. Let's use utf-8 and escape unkown bytes.
Ref. https://forum.mikrotik.com/viewtopic.php?t=106053#p528460

It is possible to use specific encoding by defining custom default structure like this:

import collections
import routeros_api
from routeros_api.api_structure import StringField

connection = routeros_api.RouterOsApiPool('ip', username='admin', password='password', plaintext_login=True)
api = connection.get_api()
# This part here is important:
default_structure = collections.defaultdict(lambda: StringField(encoding='windows-1250'))
api.get_resource('/system/identity', structure=default_structure).get()

Fixes: #53
Fixes: #68

Supersedes: #58
Supersedes: #69
Supersedes: #74
Supersedes: #82
Supersedes: #90
Supersedes: #101

@jgoclawski jgoclawski merged commit 212cf8a into master Mar 7, 2025
4 checks passed
@jgoclawski jgoclawski deleted the fix_non_utf8 branch March 7, 2025 10:38
@levonyan021
Copy link

Thank you

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

Successfully merging this pull request may close these issues.

Unicode Decode Error (UTF-8) api_structure.py utf-8 code

3 participants