Search code examples
apirequestresponseshopware

Shopware Product API response with no content and error 204 (No Content) Error


So while trying to test how to create a product, with as minimal data as possible, using the Shopware 6 API i get a Status 204 (No Content) response with an empty response.
When i press "Send" again i get a Status 400 error with this message:

{
    "errors": [{
        "status": "400",
        "code": "CONTENT__DUPLICATE_PRODUCT_NUMBER",
        "title": "Bad Request",
        "detail": "Product with number \u002212345667891\u0022 already exists.",
        "meta": {
            "parameters": {
                "number": "12345667891"
            }
        }
    }]
}

When i check the Shopware Backend and search for the product, i get no result. When i try searching the MySQL Product-Tables i get no result aswell.

Iam using this site: https://reqbin.com/
And I call the URL: https://www.my-url.de/api/product via POST Request.
As authorization i use a bearer token i grab from the getToken GET-Request.
My Request-Content looks like the following:

{
  "name": "Aarons Testartikel of Doom",
  "taxId": "f68a9c3c86864c2ea7597062fc457cf7",
  "price": 3,
  "productNumber": "12345667891",
  "stock": 5,
  "active": true
}

Solution

  • The 204 status code you get on your first request indicates that the request was handled successful. According to the HTTP-Standard all 2xx status codes can be considered successful. Shopware by default returns a 204 - No Content with en empty response body to all write requests over the API, but you can provide a _response query parameter to your POST-Request and Shopware will return a 200 response with the same data you'll get if you make a GET-Request to read the data of the added product.

    In your case the URL would be https://www.my-url.de/api/product?_response=1.

    The reason for the Error on the second request is that the product number needs to be unique, so you can't add another product with the same product number. Changing the product number before you do the second request should work again.

    The Error also indicates that the first Write-Request was indeed successful, to make sure that your shopware installation is connected to the Database you think it is, you can check the .env file in the root folder of your installation, there should be a DATABASE_URL variable defined.