Skip to content Skip to sidebar Skip to footer

Python Requests - Encoding With 'idna' Codec Failed (unicodeerror: Label Empty Or Too Long) Error

An api call I have been using with the requests package is suddenly returning the following error: 'UnicodeError: encoding with 'idna' codec failed (UnicodeError: label empty or to

Solution 1:

It seems this is an issue from the socket module. It fails when the URL exceeds 64 characters. This is still an open issue https://bugs.python.org/issue32958

The error can be consistently reproduced when the first substring of the url hostname is greater than 64 characters long, as in "0123456789012345678901234567890123456789012345678901234567890123.example.com". This wouldn't be a problem, except that it doesn't seem to separate out credentials from the first substring of the hostname so the entire "[user]:[secret]@XXX" section must be less than 65 characters long. This is problematic for services that use longer API keys and expect their submission over basic auth.

There is an alternative solution:

It seems you're trying to use the Shopify API so I'll take it as an example.

Encode {api_key}:{password} in base64 and send this value in the headers of your request eg. {'Authorization': 'Basic {token_base_64}'}

See the example below:

import base64
import requests

auth = "[API KEY]:[PASSWORD]"
b64_auth = base64.b64encode(auth.encode()).decode("utf-8")

headers = {
    "Authorization": f"Basic {b64_auth}"
}

response = requests.get(
    url="https://[YOUR-SHOP].myshopify.com/admin/[ENDPOINT]",
    headers=headers
)

Post a Comment for "Python Requests - Encoding With 'idna' Codec Failed (unicodeerror: Label Empty Or Too Long) Error"