A space to discuss GraphQL queries, mutations, troubleshooting, throttling, and best practices.
Hello,
I'm having troubles to declare variables in graphql and send them via curl. On this forum I found this post, but it wasn't closed and still doesn't solve my problem
Whenever I use the Content-Type defined as application/json instead of application/graphql, Shopify sends me back the error Bad Request
For example with this curl
curl -X POST \
https://my-site.myshopify.com/admin/api/2022-04/graphql.json \
-H 'Content-Type: application/json' \
-H 'X-Shopify-Access-Token: my-token' \
-d '{
"query": "mutation productUpdate($input: ProductInput!) {
productUpdate(input: $input) {
product {
id
}
userErrors {
field
message
}
}
}",
"variables":{
"input":
{
"id": "gid://shopify/Product/{product_id}",
"title": "New title",
"descriptionHtml": "<p>Update from GraphQL Admin API</p>"
}
}
}'
I get back the same message Bad Request also it I try the curl on the Shopify Docs here https://shopify.dev/api/admin-graphql/2022-04/mutations/productUpdate#examples-Update_a_product_s_ti...
Instead, if I use the the Content-Type as application/graphql, I get the same problem of the post I linked here above, something like
{"errors":[{"message":"Parse error on \"query\" (STRING) at [3, 5]","locations":[{"line":2,"column":2}]}]}
Thank you for any input!
Solved! Go to the solution
This is an accepted solution.
Looks like it only works if the query part does not contain newlines.
In the API documentation are also all examples without newlines in the query part.
curl -X POST \
https://my-site.myshopify.com/admin/api/2022-04/graphql.json \
-H 'Content-Type: application/json' \
-H 'X-Shopify-Access-Token: my-token' \
-d '{
"query": "mutation productUpdate($input: ProductInput!) { productUpdate(input: $input) { product { id } userErrors { field message } } }",
"variables":{
"input":
{
"id": "gid://shopify/Product/{product_id}",
"title": "New title",
"descriptionHtml": "<p>Update from GraphQL Admin API</p>"
}
}
}'
This is an accepted solution.
Looks like it only works if the query part does not contain newlines.
In the API documentation are also all examples without newlines in the query part.
curl -X POST \
https://my-site.myshopify.com/admin/api/2022-04/graphql.json \
-H 'Content-Type: application/json' \
-H 'X-Shopify-Access-Token: my-token' \
-d '{
"query": "mutation productUpdate($input: ProductInput!) { productUpdate(input: $input) { product { id } userErrors { field message } } }",
"variables":{
"input":
{
"id": "gid://shopify/Product/{product_id}",
"title": "New title",
"descriptionHtml": "<p>Update from GraphQL Admin API</p>"
}
}
}'
Thanks a lot!