'variables': 'expected String to be a Hash'}} Error when use python to translate language

feiyutech
New Member
2 0 0

Hi I try to use the python to translate the language with GraphQL API. but show {'errors': {'variables': 'expected String to be a Hash'}}.  Any one know what is wrong? Thank you very much. This work without the variables

from setting import API_KEY, PASSWORD,API_VERSION,SHOP_NAME,SHARED_SECRET
import requests

headers = {
        'accept':"application/json",
        'Content-Type':'application/json',
        'X-Shopify-Access-Token': PASSWORD
    }

query='''
mutation CreateTranslation($id: ID!, $translations: [TranslationInput!]!) {
  translationsRegister(resourceId: $id, translations: $translations) {
    userErrors {
      message
      field
    }
    translations {
      locale
      key
      value
    }
  }
}
    '''
variables='''
  {
  "id": "gid://shopify/Link/779818184",
  "translations": [
    {
      "key": "title",
      "value": "Produkte",
      "locale": "de",
      "translatableContentDigest": "4edc8bfafc6bd936b849f38c009454eac0febc8982f990e734724156584d56f9"
    }
  ]
}
'''

r=requests.post("https://feiyutech.myshopify.com/admin/api/2020-01/graphql.json",json={'query':query,'variables':variables},headers=headers)

Array=r.json()
print(Array)
0 Likes
SBD_
Shopify Staff
Shopify Staff
1081 146 193

Hey @feiyutech,

 

Sounds like your variables are coming through as a string.

 

I'm not familiar with Python, but some threads to follow here: https://community.shopify.com/c/Shopify-APIs-SDKs/Python-GraphQL-query-with-variables/td-p/541545

 

Let me know if you get stuck.

 

AJ_MotorSport
Excursionist
34 0 4

NOTE: The below information is wrong.

I will keep it here and update it when I find a solution.

The message below is WRONG!

=================================

I received this message:

'variables': 'expected String to be a Hash'}}

When trying to use variables in Shopify's GraphQL API using the shopify_python_api library.

I had no idea *where* to place the variables.

Hoping this helps others:

The variables go at the end of your query.

So say you have

query_for_api = """
mutations and stuff.... {
}

"variables" : { whatever it is you put here... maybe from the GraphiQL application.
}
"""

Then just send the entire query to be executed:

client.execute(query_for_api)

I hope this helps someone.

 

EOL

0 Likes
Jupiyter
New Member
1 0 0

Hello Guys,

 

I'm also trying to solve this without any progress. Is there any solution?

 

I read the threads posted here and other sources. 

 

Here is what I created until now:

 

The params:

{
    "input": {
        "descriptionHtml": "xxx",
        "title": "xxx",
        "tags": "xxx",
        "variants": [
            {
                "title": "sport Layouts Shiney TightsDamen gr\u00fcn Gr\u00f6\u00dfe XS",
                "barcode": "7314840007567",
                "price": "39.29",
                "options": [
                    "XS"
                ]
            }
        ]
    }
}

The requests I send:

response = requests.post(shop_url, json={"query":query, "variables": param})

 

0 Likes
AJ_MotorSport
Excursionist
34 0 4

Check this answer out, on this thread: GraphQL Variables Help Please

 

I asked over there, and one of the Shopify Staff replied and explained how to do it.

0 Likes
PublicApps
Shopify Partner
88 4 24

Make sure to inspect the request that is eventually sent to Shopify (by your backend code or by the shopify-graphql-proxy package if you're using that).

It should be a POST request to https://<myshop>.myshopify.com/admin/api/<version, e.g. 2020-10>/graphql.json
The payload should be JSON with two keys: "query" or "mutation" (its value should be a string) and "variables" (its value should be an object):

 

{
	"query": "some string",
	"variables": {
		"var1": 1,
		"var2": 2
	}
}

 

The headers should be

 

Content-Type: application/json
X-Shopify-Access-Token: <the_access_token>

 

Often times the payload is formatted incorrectly. Do not stringify JSON strings. E.g. when using Python's requests.post(url, json=my_payload) the payload and Content-Type are automatically converted to JSON, so my_payload should be a Python object in this example.

Just trying to help out.
- If my reply was helpful, please Like it or mark it as Accepted Solution to let me know!
- Need store customizations? Then consider Hiring us.
0 Likes