Artikel anlegen

Hallo, ich habe bisher alle Artikel aus dem Shop über die API ausgelesen restRequest = new RestRequest("articles/{id}?useNumberAsId=true", Method.GET); restRequest.AddUrlSegment("id", artikel.ArtikelNummer.ToString(CultureInfo.InvariantCulture)); restRequest.AddHeader("Content-Type", "application/json; charset=utf-8"); iRestResponse = restClient.Execute(restRequest); if(null != iRestResponse.ErrorException) { } else { rootObject = iRestResponse.Content.ConvertToJsonClassObject(); // Auslesen eines Artikels if(null != rootObject) { if(rootObject.Success) { lArtikelAendern.Add(artikel); } else { lArtikelAnlegen.Add(artikel); } } } Wenn ich nun anlegen will oder updaten bekomme ich immer „validation error“ „name“ „…not be blank“ restRequest = new RestRequest("articles/", Method.PUT); restRequest.AddParameter("application/json", rootObjecte.ConvertToJsonString(), ParameterType.RequestBody); restRequest.RequestFormat = DataFormat.Json; //restRequest = new RestRequest("articles/", Method.POST); //restRequest.AddHeader("Content-Type", "text/json"); //restRequest.AddParameter("text/json", rootObjecte.ConvertToJsonString(), ParameterType.RequestBody); iRestResponse = restClient.Execute(restRequest); Was mach ich Falsch? Ich verwende VS2010 mit RestSharp und Newton.Json, verbinden möchte ich mich zu Shopware 4.2

Hallo, Wie sieht denn die Fehlermeldung konkret aus?

Hallo, Json String { "data": [{ "active": true, "added": "17.03.2014 09:00:03", "availableFrom": null, "availableTo": null, "categories": { "categories": [ { "id": -1, "name": "50" }, { "id": -1, "name": "50110" }, { "id": -1, "name": "nb" }], "related": null, "similar": null }, "changed": null, "configuratorSet": null, "configuratorSetId": null, "crossBundleLook": 0, "customerGroups": null, "description": "Apple iPad 2 Wi-Fi + 3G, 16 GB, weiss MC982FD/A", "descriptionLong": null, "details": null, "downloads": null, "filterGroupId": null, "highlight": false, "id": -1, "images": [{ "articleDetailId": null, "articleId": -1, "description": "132988", "extension": null, "height": 0, "id": -1, "main": 0, "mediaId": -1, "parentId": null, "path": "http://img.bilderpfad.de/bilderexport/132988.jpg", "position": 0, "relations": null, "width": 0 }], "keywords": null, "lastStock": false, "links": null, "mainDetail": { "active": 1, "additionalText": null, "articleId": -1, "attribute": { "articleDetailId": -1, "articleId": -1, "attr1": "Hersteller Artikel-Nr. 'MC982FD/A'", "attr10": null, "attr11": null, "attr12": null, "attr13": null, "attr14": null, "attr15": null, "attr16": null, "attr17": null, "attr18": null, "attr19": null, "attr2": null, "attr20": null, "attr3": null, "attr4": null, "attr5": null, "attr6": null, "attr7": null, "attr8": null, "attr9": null, "id": -1 }, "configuratorOptions": null, "ean": "885909470747", "height": 0.0, "id": -1, "categories": 0, "kind": 0, "len": 0.0, "maxPurchase": null, "minPurchase": null, "number": "132988", "packUnit": null, "position": 0, "prices": [], "purchaseSteps": null, "purchaseUnit": null, "referenceUnit": null, "releaseDate": null, "shippingFree": false, "shippingTime": null, "stockMin": null, "supplierNumber": "MC982FD/A", "unitId": null, "weight": "1", "width": 0.0 }, "mainDetailId": -1, "metaTitle": null, "mode": 0, "name": "Apple iPad 2 Wi-Fi + 3G, 16 GB, weiss MC982FD/A", "notification": false, "priceGroupActive": false, "priceGroupId": null, "propertyGroup": null, "propertyValues": null, "pseudoSales": 0, "related": null, "similar": null, "supplier": { "description": null, "id": -1, "image": null, "link": null, "metaDescription": null, "metaKeywords": null, "metaTitle": null, "name": "Apple" }, "supplierId": -1, "tax": null, "taxId": 1, "template": null }, { "active": true, "added": "17.03.2014 09:00:03", "availableFrom": null, "availableTo": null, "categories": { "categories": [{ "id": -1, "name": "50" }, { "id": -1, "name": "50110" }, { "id": -1, "name": "nb" }], "related": null, "similar": null }, "changed": null, "configuratorSet": null, "configuratorSetId": null, "crossBundleLook": 0, "customerGroups": null, "description": "Apple iPad 2 Wi-Fi + 3G, 16 GB, schwarz MC773FD/A", "descriptionLong": null, "details": null, "downloads": null, "filterGroupId": null, "highlight": false, "id": -1, "images": [{ "articleDetailId": null, "articleId": -1, "description": "132990", "extension": null, "height": 0, "id": -1, "main": 0, "mediaId": -1, "parentId": null, "path": "http://img.bilderpfad.de/bilderexport/132990.jpg", "position": 0, "relations": null, "width": 0 }], "keywords": null, "lastStock": false, "links": null, "mainDetail": { "active": 1, "additionalText": null, "articleId": -1, "attribute": { "articleDetailId": -1, "articleId": -1, "attr1": "Hersteller Artikel-Nr. 'MC773FD/A'", "attr10": null, "attr11": null, "attr12": null, "attr13": null, "attr14": null, "attr15": null, "attr16": null, "attr17": null, "attr18": null, "attr19": null, "attr2": null, "attr20": null, "attr3": null, "attr4": null, "attr5": null, "attr6": null, "attr7": null, "attr8": null, "attr9": null, "id": -1 }, "configuratorOptions": null, "ean": "885909466443", "height": 0.0, "id": -1, "categories": 0, "kind": 0, "len": 0.0, "maxPurchase": null, "minPurchase": null, "number": "132990", "packUnit": null, "position": 0, "prices": [], "purchaseSteps": null, "purchaseUnit": null, "referenceUnit": null, "releaseDate": null, "shippingFree": false, "shippingTime": null, "stockMin": null, "supplierNumber": "MC773FD/A", "unitId": null, "weight": "1", "width": 0.0 }, "mainDetailId": -1, "metaTitle": null, "mode": 0, "name": "Apple iPad 2 Wi-Fi + 3G, 16 GB, schwarz MC773FD/A", "notification": false, "priceGroupActive": false, "priceGroupId": null, "propertyGroup": null, "propertyValues": null, "pseudoSales": 0, "related": null, "similar": null, "supplier": { "description": null, "id": -1, "image": null, "link": null, "metaDescription": null, "metaKeywords": null, "metaTitle": null, "name": "Apple" }, "supplierId": -1, "tax": null, "taxId": 1, "template": null } ], "total": 2, "message": null, "success": false } Zum Ausführen restRequest = new RestRequest("articles/", Method.POST); restRequest.AddHeader("Content-Type", "application/json; charset=utf-8"); restRequest.AddParameter("application/json", rootObjecte.ConvertToJsonString(), ParameterType.RequestBody); restRequest.RequestFormat = DataFormat.Json; iRestResponse = restClient.Execute(restRequest); Die Meldung { "success": true, "data": { "data": { "success": false, "message": "Shopware\\Models\\Article\\Article.name:\n This value should not be blank.\nShopware\\Models\\Article\\Article.tax:\n This value should not be blank.\nShopware\\Models\\Article\\Article.mainDetail.number:\n This value should not be blank.", "trace": "#0 \/var\/www\/shops\/sz\_test\/engine\/Shopware\/Components\/Api\/Resource\/Resource.php(523): Shopware\\Components\\Api\\Resource\\Article-\>create(Array)\n#1 \/var\/www\/shops\/sz\_test\/engine\/Shopware\/Controllers\/Api\/Rest.php(79): Shopware\\Components\\Api\\Resource\\Resource-\>batch(Array)\n#2 \/var\/www\/shops\/sz\_test\/engine\/Library\/Enlight\/Controller\/Action.php(159): Shopware\_Controllers\_Api\_Rest-\>batchAction()\n#3 \/var\/www\/shops\/sz\_test\/engine\/Library\/Enlight\/Controller\/Dispatcher\/Default.php(528): Enlight\_Controller\_Action-\>dispatch('batchAction')\n#4 \/var\/www\/shops\/sz\_test\/engine\/Library\/Enlight\/Controller\/Front.php(228): Enlight\_Controller\_Dispatcher\_Default-\>dispatch(Object(Enlight\_Controller\_Request\_RequestHttp), Object(Enlight\_Controller\_Response\_ResponseHttp))\n#5 \/var\/www\/shops\/sz\_test\/engine\/Shopware\/Kernel.php(141): Enlight\_Controller\_Front-\>dispatch()\n#6 \/var\/www\/shops\/sz\_test\/vendor\/symfony\/http-kernel\/Symfony\/Component\/HttpKernel\/HttpCache\/HttpCache.php(473): Shopware\\Kernel-\>handle(Object(Symfony\\Component\\HttpFoundation\\Request), 1, true)\n#7 \/var\/www\/shops\/sz\_test\/engine\/Shopware\/Components\/HttpCache\/AppCache.php(256): Symfony\\Component\\HttpKernel\\HttpCache\\HttpCache-\>forward(Object(Symfony\\Component\\HttpFoundation\\Request), true, NULL)\n#8 \/var\/www\/shops\/sz\_test\/vendor\/symfony\/http-kernel\/Symfony\/Component\/HttpKernel\/HttpCache\/HttpCache.php(244): Shopware\\Components\\HttpCache\\AppCache-\>forward(Object(Symfony\\Component\\HttpFoundation\\Request), true)\n#9 \/var\/www\/shops\/sz\_test\/vendor\/symfony\/http-kernel\/Symfony\/Component\/HttpKernel\/HttpCache\/HttpCache.php(261): Symfony\\Component\\HttpKernel\\HttpCache\\HttpCache-\>pass(Object(Symfony\\Component\\HttpFoundation\\Request), true)\n#10 \/var\/www\/shops\/sz\_test\/engine\/Shopware\/Components\/HttpCache\/AppCache.php(132): Symfony\\Component\\HttpKernel\\HttpCache\\HttpCache-\>invalidate(Object(Symfony\\Component\\HttpFoundation\\Request), true)\n#11 \/var\/www\/shops\/sz\_test\/vendor\/symfony\/http-kernel\/Symfony\/Component\/HttpKernel\/HttpCache\/HttpCache.php(190): Shopware\\Components\\HttpCache\\AppCache-\>invalidate(Object(Symfony\\Component\\HttpFoundation\\Request), true)\n#12 \/var\/www\/shops\/sz\_test\/engine\/Shopware\/Components\/HttpCache\/AppCache.php(113): Symfony\\Component\\HttpKernel\\HttpCache\\HttpCache-\>handle(Object(Symfony\\Component\\HttpFoundation\\Request), 1, true)\n#13 \/var\/www\/shops\/sz\_test\/shopware.php(91): Shopware\\Components\\HttpCache\\AppCache-\>handle(Object(Symfony\\Component\\HttpFoundation\\Request))\n#14 {main}" }, "total": { "success": false, "message": "Shopware\\Models\\Article\\Article.name:\n This value should not be blank.\nShopware\\Models\\Article\\Article.tax:\n This value should not be blank.\nShopware\\Models\\Article\\Article.mainDetail.number:\n This value should not be blank.", "trace": "#0 \/var\/www\/shops\/sz\_test\/engine\/Shopware\/Components\/Api\/Resource\/Resource.php(523): Shopware\\Components\\Api\\Resource\\Article-\>create(22)\n#1 \/var\/www\/shops\/sz\_test\/engine\/Shopware\/Controllers\/Api\/Rest.php(79): Shopware\\Components\\Api\\Resource\\Resource-\>batch(Array)\n#2 \/var\/www\/shops\/sz\_test\/engine\/Library\/Enlight\/Controller\/Action.php(159): Shopware\_Controllers\_Api\_Rest-\>batchAction()\n#3 \/var\/www\/shops\/sz\_test\/engine\/Library\/Enlight\/Controller\/Dispatcher\/Default.php(528): Enlight\_Controller\_Action-\>dispatch('batchAction')\n#4 \/var\/www\/shops\/sz\_test\/engine\/Library\/Enlight\/Controller\/Front.php(228): Enlight\_Controller\_Dispatcher\_Default-\>dispatch(Object(Enlight\_Controller\_Request\_RequestHttp), Object(Enlight\_Controller\_Response\_ResponseHttp))\n#5 \/var\/www\/shops\/sz\_test\/engine\/Shopware\/Kernel.php(141): Enlight\_Controller\_Front-\>dispatch()\n#6 \/var\/www\/shops\/sz\_test\/vendor\/symfony\/http-kernel\/Symfony\/Component\/HttpKernel\/HttpCache\/HttpCache.php(473): Shopware\\Kernel-\>handle(Object(Symfony\\Component\\HttpFoundation\\Request), 1, true)\n#7 \/var\/www\/shops\/sz\_test\/engine\/Shopware\/Components\/HttpCache\/AppCache.php(256): Symfony\\Component\\HttpKernel\\HttpCache\\HttpCache-\>forward(Object(Symfony\\Component\\HttpFoundation\\Request), true, NULL)\n#8 \/var\/www\/shops\/sz\_test\/vendor\/symfony\/http-kernel\/Symfony\/Component\/HttpKernel\/HttpCache\/HttpCache.php(244): Shopware\\Components\\HttpCache\\AppCache-\>forward(Object(Symfony\\Component\\HttpFoundation\\Request), true)\n#9 \/var\/www\/shops\/sz\_test\/vendor\/symfony\/http-kernel\/Symfony\/Component\/HttpKernel\/HttpCache\/HttpCache.php(261): Symfony\\Component\\HttpKernel\\HttpCache\\HttpCache-\>pass(Object(Symfony\\Component\\HttpFoundation\\Request), true)\n#10 \/var\/www\/shops\/sz\_test\/engine\/Shopware\/Components\/HttpCache\/AppCache.php(132): Symfony\\Component\\HttpKernel\\HttpCache\\HttpCache-\>invalidate(Object(Symfony\\Component\\HttpFoundation\\Request), true)\n#11 \/var\/www\/shops\/sz\_test\/vendor\/symfony\/http-kernel\/Symfony\/Component\/HttpKernel\/HttpCache\/HttpCache.php(190): Shopware\\Components\\HttpCache\\AppCache-\>invalidate(Object(Symfony\\Component\\HttpFoundation\\Request), true)\n#12 \/var\/www\/shops\/sz\_test\/engine\/Shopware\/Components\/HttpCache\/AppCache.php(113): Symfony\\Component\\HttpKernel\\HttpCache\\HttpCache-\>handle(Object(Symfony\\Component\\HttpFoundation\\Request), 1, true)\n#13 \/var\/www\/shops\/sz\_test\/shopware.php(91): Shopware\\Components\\HttpCache\\AppCache-\>handle(Object(Symfony\\Component\\HttpFoundation\\Request))\n#14 {main}" }, "success": { "success": false, "message": "Shopware\\Models\\Article\\Article.name:\n This value should not be blank.\nShopware\\Models\\Article\\Article.tax:\n This value should not be blank.\nShopware\\Models\\Article\\Article.mainDetail.number:\n This value should not be blank.", "trace": "#0 \/var\/www\/shops\/sz\_test\/engine\/Shopware\/Components\/Api\/Resource\/Resource.php(523): Shopware\\Components\\Api\\Resource\\Article-\>create(false)\n#1 \/var\/www\/shops\/sz\_test\/engine\/Shopware\/Controllers\/Api\/Rest.php(79): Shopware\\Components\\Api\\Resource\\Resource-\>batch(Array)\n#2 \/var\/www\/shops\/sz\_test\/engine\/Library\/Enlight\/Controller\/Action.php(159): Shopware\_Controllers\_Api\_Rest-\>batchAction()\n#3 \/var\/www\/shops\/sz\_test\/engine\/Library\/Enlight\/Controller\/Dispatcher\/Default.php(528): Enlight\_Controller\_Action-\>dispatch('batchAction')\n#4 \/var\/www\/shops\/sz\_test\/engine\/Library\/Enlight\/Controller\/Front.php(228): Enlight\_Controller\_Dispatcher\_Default-\>dispatch(Object(Enlight\_Controller\_Request\_RequestHttp), Object(Enlight\_Controller\_Response\_ResponseHttp))\n#5 \/var\/www\/shops\/sz\_test\/engine\/Shopware\/Kernel.php(141): Enlight\_Controller\_Front-\>dispatch()\n#6 \/var\/www\/shops\/sz\_test\/vendor\/symfony\/http-kernel\/Symfony\/Component\/HttpKernel\/HttpCache\/HttpCache.php(473): Shopware\\Kernel-\>handle(Object(Symfony\\Component\\HttpFoundation\\Request), 1, true)\n#7 \/var\/www\/shops\/sz\_test\/engine\/Shopware\/Components\/HttpCache\/AppCache.php(256): Symfony\\Component\\HttpKernel\\HttpCache\\HttpCache-\>forward(Object(Symfony\\Component\\HttpFoundation\\Request), true, NULL)\n#8 \/var\/www\/shops\/sz\_test\/vendor\/symfony\/http-kernel\/Symfony\/Component\/HttpKernel\/HttpCache\/HttpCache.php(244): Shopware\\Components\\HttpCache\\AppCache-\>forward(Object(Symfony\\Component\\HttpFoundation\\Request), true)\n#9 \/var\/www\/shops\/sz\_test\/vendor\/symfony\/http-kernel\/Symfony\/Component\/HttpKernel\/HttpCache\/HttpCache.php(261): Symfony\\Component\\HttpKernel\\HttpCache\\HttpCache-\>pass(Object(Symfony\\Component\\HttpFoundation\\Request), true)\n#10 \/var\/www\/shops\/sz\_test\/engine\/Shopware\/Components\/HttpCache\/AppCache.php(132): Symfony\\Component\\HttpKernel\\HttpCache\\HttpCache-\>invalidate(Object(Symfony\\Component\\HttpFoundation\\Request), true)\n#11 \/var\/www\/shops\/sz\_test\/vendor\/symfony\/http-kernel\/Symfony\/Component\/HttpKernel\/HttpCache\/HttpCache.php(190): Shopware\\Components\\HttpCache\\AppCache-\>invalidate(Object(Symfony\\Component\\HttpFoundation\\Request), true)\n#12 \/var\/www\/shops\/sz\_test\/engine\/Shopware\/Components\/HttpCache\/AppCache.php(113): Symfony\\Component\\HttpKernel\\HttpCache\\HttpCache-\>handle(Object(Symfony\\Component\\HttpFoundation\\Request), 1, true)\n#13 \/var\/www\/shops\/sz\_test\/shopware.php(91): Shopware\\Components\\HttpCache\\AppCache-\>handle(Object(Symfony\\Component\\HttpFoundation\\Request))\n#14 {main}" } } }

So ich hab mal das ganze auf den Pflichfelder begrenzt Aufruf restRequest = new RestRequest("articles/", Method.POST); restRequest.AddParameter("application/json", rootObject.ConvertToJsonString(), ParameterType.RequestBody); restRequest.RequestFormat = DataFormat.Json; iRestResponse = restClient.Execute(restRequest); JSON { "data": { "name": "Apple iPad 2 Wi-Fi + 3G, 16 GB, weiss MC982FD/A", "tax": 19, "mainDetail": { "number": "132988" } } } oder auch mal mit { "data": { "name": "Apple iPad 2 Wi-Fi + 3G, 16 GB, weiss MC982FD/A", "taxId": 1, "mainDetail": { "number": "132988" } } } Ergebnis { "success": false, "message": "Validation error", "errors": ["name: This value should not be blank.", "tax: This value should not be blank.", "mainDetail.number: This value should not be blank."] }

Hallo, Ok nun hab ich das ganze Problem gefunden { "data": { "name": "Apple iPad 2 Wi-Fi + 3G, 16 GB, weiss MC982FD/A", "taxId": 1, "mainDetail": { "number": "132988" } } } muss zu { "name": "Apple iPad 2 Wi-Fi + 3G, 16 GB, weiss MC982FD/A", "taxId": 1, "mainDetail": { "number": "132988" } } Leider funktioniert dies hier nicht um mehrere Artikel anzulegen [{ "name": "Apple iPad 2 Wi-Fi + 3G, 16 GB, weiss MC982FD/A", "taxId": 1, "mainDetail": { "number": "132988" } }, { "name": "Apple iPad 2 Wi-Fi + 3G, 32 GB, weiss MC982FD/C", "taxId": 1, "mainDetail": { "number": "132989" } }] Wie muss ich nun vorgehen damit ich ein Array von Artikel anlegen kann? Objekt zu JSON String mittels JsonConvert.SerializeObject(rootObjecte).ToString(CultureInfo.InvariantCulture)

Hallo, ich habe das selbe Problem und bin ihm scheinbar auf die Schliche gekommen. In der engine\Library\Enlight\Controller\Dispatcher\Default.php wird in Zeile 517 das Http Verb aus dem Enlight Controller Request in $action gezogen. Um einen POST per Stapelverarbeitung abzuarbeiten müsste hier der String „batchAction“ ankommen. Ähnlich wie bei der funktionierenden Stapelverarbeitung des Verbs PUT. Es kommt jedoch immer „post“ an. Eine Lösung hab ich leider noch nicht gefunden. Wenn ich allerdings dort ‚batchAction‘ von Hand eintrage läuft die POST Stapelverarbeitung aber problemlos.

Hallo, ich hab das Problem und auch eine improvisierte Lösung gefunden. Die „Action“ wird in dem Core RestApi Plugin unter \engine\Shopware\Plugins\Default\Core\RestApi\Components\Router.php gesetzt. Dort fehlt die Programmweiche für den batchmodus beim POST. Eine korrekte Lösung wäre z. B. in den Request Feldern zu prüfen ob ein Array von Artikeln gepostet wurde oder nur ein Artikel angelegt werden soll. Als improvisierte Lösung habe ich mal den ID Parameter als Unterscheidungsmerkmals genommen, das ist sicherlich nicht die sauberste Lösung, sie funktioniert aber. Ab Zeile 78 einfach eine weitere elseif Anweisung vor dem eigentlichen POST einfügen: } elseif ($method === ‚POST‘ && $id === true) { # ist in der URL ein Parameter ID angegeben, $action = ‚batch‘; # nutze batchmodus $response->setHttpResponseCode(201); } elseif ($method === ‚POST‘) { $action = ‚post‘; // Set default http status code for successfull request $response->setHttpResponseCode(201); } der Parameter wird nur zur Identifikation des Modus verwendet, der Wert selbst hat also kein Belang

Korrektur: Sehe gerade mit „$id === true“ geht es nicht, allerdings mit „$id === false“ Es muss dann so aussehen: } elseif ($method === ‚POST‘ && $id === false) { # wenn KEINE ID in der URL übergeben wird $action = ‚batch‘; # nutze Batchmodus $response->setHttpResponseCode(201); } elseif ($method === ‚POST‘) { $action = ‚post‘; // Set default http status code for successfull request $response->setHttpResponseCode(201);

Hier die korrekte Lösung, einfach an oben genannte Stelle einarbeiten: } elseif ($method === ‘POST’ && is_array(json_decode($request->getRawBody(),true)[0])) { $action = ‘batch’; $response->setHttpResponseCode(201);