You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Curl+GraphQL向Shopify插入HTML翻译遇字符转义问题

Shopify GraphQL插入HTML格式产品翻译的解决方案

问题本质

你遇到的<字符报错,是shell解析Curl命令时误处理了HTML标签的特殊字符,而非Shopify GraphQL接口不支持HTML内容——body_html字段本身允许传入HTML格式,只需确保请求在传输过程中结构正确。

可行解决方案

1. 用单引号包裹Curl中的JSON字符串

在Curl的-d参数中用单引号包裹整个JSON内容,避免shell解析<、>等字符。若HTML内容包含单引号,需转义为'\'':

curl -X POST \
  https://your-shop.myshopify.com/admin/api/2024-07/graphql.json \
  -H "Content-Type: application/json" \
  -H "X-Shopify-Access-Token: your-access-token" \
  -d '{
    "query": "mutation CreateTranslation($id: ID!, $translations: [TranslationInput!]!) { translationsRegister(resourceId: $id, translations: $translations) { userErrors {message field} translations {key value locale} } }",
    "variables": {
      "id": "gid://shopify/Product/myid",
      "translations": [
        {
          "key": "body_html",
          "value": "<p>La Camicia da Uomo in Cotone con Colletto Francese in Blu Chiaro, tailor fit.  Questa camicia è più di un semplice capo d'\''abbigliamento; è una dichiarazione di gusto raffinato, progettata per elevare il tuo stile in una varietà di occasioni.</p><ul><li>...</li></ul>",
          "locale": "it",
          "translatableContentDigest": "73f987270db7311a720b001d6155d8f008db796b7341d1c59177a6c02ab129c2"
        }
      ]
    }
  }'

2. 将JSON请求写入文件,通过Curl读取发送

把请求内容保存到独立的JSON文件(比如translation-request.json),彻底规避shell转义问题:

{
  "query": "mutation CreateTranslation($id: ID!, $translations: [TranslationInput!]!) { translationsRegister(resourceId: $id, translations: $translations) { userErrors {message field} translations {key value locale} } }",
  "variables": {
    "id": "gid://shopify/Product/myid",
    "translations": [
      {
        "key": "body_html",
        "value": "<p>La Camicia da Uomo in Cotone con Colletto Francese in Blu Chiaro, tailor fit.  Questa camicia è più di un semplice capo d'abbigliamento; è una dichiarazione di gusto raffinato, progettata per elevare il tuo stile in una varietà di occasioni.</p><ul><li>...</li></ul>",
        "locale": "it",
        "translatableContentDigest": "73f987270db7311a720b001d6155d8f008db796b7341d1c59177a6c02ab129c2"
      }
    ]
  }
}

执行Curl命令读取文件发送:

curl -X POST \
  https://your-shop.myshopify.com/admin/api/2024-07/graphql.json \
  -H "Content-Type: application/json" \
  -H "X-Shopify-Access-Token: your-access-token" \
  -d @translation-request.json

3. 验证translatableContentDigest的正确性

若以上操作仍报错,检查translatableContentDigest是否与产品当前原始语言的body_html哈希值匹配——Shopify用这个值验证翻译对应最新版本。可先通过查询接口获取正确的digest:

query GetProductDigest($id: ID!) {
  product(id: $id) {
    translatableContent {
      key
      digest
    }
  }
}

变量:

{"id": "gid://shopify/Product/myid"}

内容的提问来源于stack exchange,提问作者Stefano Coccia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 21:44:49