onshape-public / onshape-clients

All Onshape clients for interacting with the Onshape API.
MIT License
32 stars 26 forks source link

"UnicodeDecodeError: 'utf-8' codec can't decode byte 0xed in position 47: invalid continuation byte" when trying to download obj from /api/documents/d/did/externaldata/fid #83

Closed jpoliniak closed 2 years ago

jpoliniak commented 2 years ago

As the title states, when I am trying to download an obj file from the /api/documents/d/did/externaldata/fid endpoint, I get a "UnicodeDecodeError: 'utf-8' codec can't decode byte 0xed in position 47: invalid continuation byte" error.

My current fix for this is to literally just comment out the two lines of code that decode the response but I was wondering if this is an actual issue or if I'm doing something wrong.

The function that I've written, for reference:

def download_obj(resultExternalID,did,filename,access_key, secret_key):
    # Instantiate the client
    base = 'https://cad.onshape.com'
    client = Client(configuration={"base_url": base,
                               "access_key": access_key,
                               "secret_key": secret_key})

    # Create URL
    fixed_url = '/api/documents/d/did/externaldata/fid'
    # did = '440f9b2f28d38a7527ddb35d'
    # resultExternalID = '6188baeead512e1ebd7f2c5b'

    fixed_url = fixed_url.replace('fid', resultExternalID)
    fixed_url = fixed_url.replace('did', did)

    # Process request
    method = 'GET'
    params = {}
    payload = {}
    headers = {'Accept': 'application/vnd.onshape.v1+json; charset=UTF-8;qs=0.1',
               'Content-Type': 'application/json'}
    response = client.api_client.request(method, url=base + fixed_url, query_params=params, headers=headers, body=payload)

    # Write request to file
    with open(filename + '.zip', "wb") as handle:
        handle.write(response.data)
jpoliniak commented 2 years ago

Just needed to add _preload_content=False into the request.