Search code examples
pythonrestshopware6

Shopware 6 REST-API product update not working


I used the post api method to create products. Now, I am trying to use the patch API to update the price of a product. However I keep getting a 400 message error "The JSON payload is malformed". I looked online for similar issues but I can't find what's wrong with my code (see below). Anyone has a suggestion?

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)

this is the error message I am receiving

{
  "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
      }
    }
  ]
}

Solution

  • At the very least, your token is incorrect. Moreover, you should use requests to handle most of the extra work you're doing here. I suspect that by passing the payload using data, you're sending direct Python data instead of a JSON payload. Use the json kwarg instead.

    import requests
    
    session = requests.Session()
    session.headers.update({"Authorization": f"Bearer {response_token.json()['access_token']}"})
    
    response = session.patch(
        "http://dev.bagger-ersatzteile.eu/api/v1/product/018d7da4eaec70fe9ebe4c0c3be1cba6",
        json={
            "price": [
                {
                    "currencyId": "b7d2554b0ce847cd82f3ac9bd1c0dfca",
                    "gross": 121,
                    "net": 100,
                    "linked": False,
                }
            ]
        },
    )
    response.raise_for_status()