Python requests error on server only. Error: encoding with 'idna' codec failed (UnicodeError: label empty or too long)

I am facing python requests error on the server only.

UnicodeError: encoding with 'idna' codec failed (UnicodeError: label empty or too long)

In the Post request, I added a query to get data through Shopify GraphQL API.

There is no error on my local machine(Windows). After successful deployment, I get this error on the server(Heroku) only.
Any solution?

1 Like

See https://stackoverflow.com/a/52744791/792084

This is a deep-in-Shopify bug interfacing with the Python Requests library (all of which are part of the dependency stack for your specific Crowdbotics app!)

You can fix this with the solution indicated above: when invoking the Shopify API, encode {api_key}:{password} in base64 and send this value in the headers of your request ie. {'Authorization': 'Basic {token_base_64}'}

Actually this solution is for Shopify Python Admin API.
Shopify API for Shopify Admin is working fine.

But, I am also working with Storefront API also.
Here is my code:

def storefront_customer_data(access_token):
    url = "https://%s.myshopify.com/api/%s/graphql.json" % (settings.SHOPIFY_SUBDOMAIN, settings.SHOPIFY_API_VERSION)
    query = """
            query
                {
                  customer(customerAccessToken: "%s") {
                    id
                    lastName
                    firstName
                    email
                  }
                }
            """ % access_token

    headers = {
        'X-Shopify-Storefront-Access-Token': str(settings.SHOPIFY_STOREFRONT_ACCESS_TOKEN),
        'Content-Type': 'application/json',
    }

    response = requests.request("POST", url, headers=headers, json={'query': query})

    res_data = response.json()

The same issue exists there. Just change the encoding on your token and it should work fine.

The Admin API needs API_KEY and Password. With these I could make the token to use in Authorization header. But, shopify storefront directly provide the token to use in header.

But, it is working on local.

The server runs slightly different versions of Python, I expect.

Server and my local both have “Python Version: 3.7.7”

@Crowdbotics_Dan @anand

What should I do? I am blocked for this error.

I tried the same thing with my personal demo app in heroku and it’s just working fine.

@tipu002, if you look at the environment variables for your production app, neither SHOPIFY_SUBDOMAIN, nor SHOPIFY_API_VERSION exist. Could this be the problem?

It works. I added them again let’s see what happens. But, I added them one by one before deploy the new feature. Sometimes I get error adding new env vars. Thanks a lot

1 Like

You are most welcome!

Adding variables only works when you have a successfully deployed backend. If it fails under different circumstances - please post here and we’ll see what’s going on.