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

Shopware 6 REST API更新商品价格失败:JSON payload格式错误

问题:PATCH API更新商品价格时返回JSON格式错误

我用POST API成功创建过商品,现在尝试用PATCH API更新ID为018d7da4eaec70fe9ebe4c0c3be1cba6的商品价格,但一直收到400错误,提示"The JSON payload is malformed"。查了资料没找到代码问题,以下是我的代码和错误信息,求解决办法:

url = "http://dev.bagger-ersatzteile.eu/api/v1/product/018d7da4eaec70fe9ebe4c0c3be1cba6"
payload = {
         "price": [
            {
                "currencyId": "b7d2554b0ce847cd82f3ac9bd1c0dfca",
                "gross": 121,
                "net": 100,
                "linked": False
            }
        ]
        }
headers = {
    "Content-Type": "application/json",
    "Accept": "application/json",
    "Authorization": response_token.json()['access_token']
}
response = requests.patch(url, data=payload, headers=headers)

收到的错误信息:

{
  "errors": [
    {
      "code": "0",
      "status": "400",
      "title": "Bad Request",
      "detail": "The JSON payload is malformed.",
      "meta": {
        "trace": [
          {
            "file": "/var/www/clients/client1/web2/web/dev/vendor/symfony/event-dispatcher/Debug/WrappedListener.php",
            "line": 116,
            "function": "onRequest",
            "class": "Shopware\\Core\\Framework\\Api\\EventListener\\JsonRequestTransformerListener",
            "type": "->"
          },
          {
            "file": "/var/www/clients/client1/web2/web/dev/vendor/symfony/event-dispatcher/EventDispatcher.php",
            "line": 220,
            "function": "__invoke",
            "class": "Symfony\\Component\\EventDispatcher\\Debug\\WrappedListener",
            "type": "->"
          },
          {
            "file": "/var/www/clients/client1/web2/web/dev/vendor/symfony/event-dispatcher/EventDispatcher.php",
            "line": 56,
            "function": "callListeners",
            "class": "Symfony\\Component\\EventDispatcher\\EventDispatcher",
            "type": "->"
          },
          {
            "file": "/var/www/clients/client1/web2/web/dev/vendor/shopware/core/Content/Flow/Dispatching/FlowDispatcher.php",
            "line": 47,
            "function": "dispatch",
            "class": "Symfony\\Component\\EventDispatcher\\EventDispatcher",
            "type": "->"
          },
          {
            "file": "/var/www/clients/client1/web2/web/dev/vendor/shopware/core/Framework/Webhook/WebhookDispatcher.php",
            "line": 70,
            "function": "dispatch",
            "class": "Shopware\\Core\\Content\\Flow\\Dispatching\\FlowDispatcher",
            "type": "->"
          },
          {
            "file": "/var/www/clients/client1/web2/web/dev/vendor/shopware/core/Framework/Event/NestedEventDispatcher.php",
            "line": 31,
            "function": "dispatch",
            "class": "Shopware\\Core\\Framework\\Webhook\\WebhookDispatcher",
            "type": "->"
          },
          {
            "file": "/var/www/clients/client1/web2/web/dev/vendor/symfony/event-dispatcher/Debug/TraceableEventDispatcher.php",
            "line": 139,
            "function": "dispatch",
            "class": "Shopware\\Core\\Framework\\Event\\NestedEventDispatcher",
            "type": "->"
          },
          {
            "file": "/var/www/clients/client1/web2/web/dev/vendor/symfony/http-kernel/HttpKernel.php",
            "line": 157,
            "function": "dispatch",
            "class": "Symfony\\Component\\EventDispatcher\\Debug\\TraceableEventDispatcher",
            "type": "->"
          },
          {
            "file": "/var/www/clients/client1/web2/web/dev/vendor/symfony/http-kernel/HttpKernel.php",
            "line": 76,
            "function": "handleRaw",
            "class": "Symfony\\Component\\HttpKernel\\HttpKernel",
            "type": "->"
          },
          {
            "file": "/var/www/clients/client1/web2/web/dev/vendor/symfony/http-kernel/Kernel.php",
            "line": 197,
            "function": "handle",
            "class": "Symfony\\Component\\HttpKernel\\HttpKernel",
            "type": "->"
          },
          {
            "file": "/var/www/clients/client1/web2/web/dev/vendor/shopware/core/HttpKernel.php",
            "line": 143,
            "function": "handle",
            "class": "Symfony\\Component\\HttpKernel\\Kernel",
            "type": "->"
          },
          {
            "file": "/var/www/clients/client1/web2/web/dev/vendor/shopware/core/HttpKernel.php",
            "line": 65,
            "function": "doHandle",
            "class": "Shopware\\Core\\HttpKernel",
            "type": "->"
          },
          {
            "file": "/var/www/clients/client1/web2/web/dev/public/index.php",
            "line": 71,
            "function": "handle",
            "class": "Shopware\\Core\\HttpKernel",
            "type": "->"
          },
          {
            "file": "/var/www/clients/client1/web2/web/dev/vendor/symfony/runtime/Runner/Symfony/HttpKernelRunner.php",
            "line": 35,
            "function": "handle",
            "class": "Symfony\\Component\\HttpKernel\\HttpKernelInterface@anonymous\\x00/var/www/clients/client1/web2/web/dev/public/index.php: 61$0",
            "type": "->"
          },
          {
            "file": "/var/www/clients/client1/web2/web/dev/vendor/autoload_runtime.php",
            "line": 29,
            "function": "run",
            "class": "Symfony\\Component\\Runtime\\Runner\\Symfony\\HttpKernelRunner",
            "type": "->"
          },
          {
            "file": "/var/www/clients/client1/web2/web/dev/public/index.php",
            "line": 14,
            "args": [
              "/var/www/clients/client1/web2/web/dev/vendor/autoload_runtime.php"
            ],
            "function": "require_once"
          }
        ],
        "file": "/var/www/clients/client1/web2/web/dev/vendor/shopware/core/Framework/Api/EventListener/JsonRequestTransformerListener.php",
        "line": 30
      }
    }
  ]
}
解决方案

问题出在requests.patch的参数使用上:你传入了data=payload,但data参数会把字典编码为表单数据,而非JSON格式。虽然你设置了Content-Type: application/json,但实际发送的内容是表单格式,导致后端解析JSON失败。

修正方法1(推荐)

把data=payload换成json=payload,requests会自动将字典序列化为JSON字符串,并正确设置Content-Type(即使你已经在headers里设置了也没问题)。

修正后的代码:

url = "http://dev.bagger-ersatzteile.eu/api/v1/product/018d7da4eaec70fe9ebe4c0c3be1cba6"
payload = {
         "price": [
            {
                "currencyId": "b7d2554b0ce847cd82f3ac9bd1c0dfca",
                "gross": 121,
                "net": 100,
                "linked": False
            }
        ]
        }
headers = {
    "Accept": "application/json",
    "Authorization": response_token.json()['access_token']
}
# 使用json参数代替data
response = requests.patch(url, json=payload, headers=headers)

修正方法2

手动将payload转为JSON字符串,用data参数发送,需要导入json模块:

import json
url = "http://dev.bagger-ersatzteile.eu/api/v1/product/018d7da4eaec70fe9ebe4c0c3be1cba6"
payload = {
         "price": [
            {
                "currencyId": "b7d2554b0ce847cd82f3ac9bd1c0dfca",
                "gross": 121,
                "net": 100,
                "linked": False
            }
        ]
        }
headers = {
    "Content-Type": "application/json",
    "Accept": "application/json",
    "Authorization": response_token.json()['access_token']
}
response = requests.patch(url, data=json.dumps(payload), headers=headers)

内容的提问来源于stack exchange,提问作者Olivier Janssen

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 19:18:10