Inhalte aufrufen

Profilbild

WebApi Add Manufacturer

webapi products manufacturers

Best Answer Marcus Gesing , 16 October 2019 - 11:54

{
  "Name": "Hello There",
  "FullDescription": "<p>Hello</p>",
  "Sku": "123456",
  "Price": "337.4300", <-- no error anymore
  "Published": true
}
A GET request lets you see the correct formatting and all required properties.
A POST must include all properties. Use PATCH for a partial update.
Go to the full post


  • Bitte melden Sie sich an, um eine Antwort zu verfassen.
7 Antworten zu diesem Thema

#1 TripleNico

TripleNico

    Advanced Member

  • Members
  • PunktPunktPunkt
  • 124 Beiträge

Geschrieben: 15 October 2019 - 14:57

When using the WebApi we first created Manufacturers by using the endpoint POST /odata/v1/ManufacturersThen we added products by using the endpoint POST /odata/v1/Products. So far so good but now we want to assign manufacturers to products by using the url: 

POST http://localhost:1260/odata/v1/Products(1)/ProductManufacturers(12) 

We do this by getting the list of manufacturers (GET /Manufacters) en getting al products (GET /Products) then we loop through each product an assing a Manufacturer to a Product. However using this syntax POST http://localhost:126...ufacturers(12) it produces an error:

 ResponseCode: 422 "Exception has been thrown by the target of an invocation."

 I think this is because we need to assign of type ProductManufacturer instead of Manufacturer

 

So the question is what is the right approach via the WebApi to create manufacturers and products and then assign manufacturers to products?



#2 Marcus Gesing

Marcus Gesing

    SmartStore AG

  • Administrators
  • 3799 Beiträge

Geschrieben: 15 October 2019 - 15:34

No, it's correct. Assigning manufacturer with ID 12 to product with ID 1. You get the product and the manufacturer ID in the response when inserting an entity, so there's no need for an additional GET request. 422 "Unprocessable Entity" occurs in this context when one or both IDs are wrong\unknown.

Marcus Gesing

Smartstore AG


#3 TripleNico

TripleNico

    Advanced Member

  • Members
  • PunktPunktPunkt
  • 124 Beiträge

Geschrieben: 16 October 2019 - 08:39

Oké, thanks for confirming it's the right method. I tested again with product (25) and manufacturer (1010), double checked and the exists so i don't know what is going wrong? Result:

POST http://someshop.com/odata/v1/Products(25)/ProductManufacturers(1010) 
ResponseCode: 422{422}
ResponseContent: 
{
  "odata.error": {
    "code": "",
    "message": {
      "lang": "en-US",
      "value": "Exception has been thrown by the target of an invocation."
}

 The product was made by the Import from CSV function and the Manufacturer was made with the WebApi.

 

 

 

Besides this i noticed that when i send a decimal or double value for example price of product then it returns "Cannot convert a primitive value to the expected type 'Edm.Decimal'"

 

Code declaration:

Public Property Price() As Global.System.Nullable(Of Decimal)

OData  Scheme:

<Property Name="Price" Type="Edm.Decimal"/>

JSON Serialize Object:

strBody = JsonConvert.SerializeObject(item, Formatting.None, New JsonSerializerSettings With {
                                                  .FloatParseHandling = FloatParseHandling.Double,
                                                  .NullValueHandling = NullValueHandling.Ignore})

JSON POST Body:

{
  "Name": "Hello There",
  "FullDescription": "<p>Hello</p>",
  "Sku": "123456",
  "Price": 337.43, <-- Error on this
  "Published": true
}

Response:

{
  "odata.error": {
    "code": "",
    "message": {
      "lang": "en-US",
      "value": "The request is invalid."
    },
    "innererror": {
      "message": "entity : Cannot convert a primitive value to the expected type 'Edm.Decimal'. See the inner exception for more details.\r\n",
      "type": "",
      "stacktrace": ""
    }
  }
}


#4 Marcus Gesing

Marcus Gesing

    SmartStore AG

  • Administrators
  • 3799 Beiträge

Geschrieben: 16 October 2019 - 11:54   Best Answer

{
  "Name": "Hello There",
  "FullDescription": "<p>Hello</p>",
  "Sku": "123456",
  "Price": "337.4300", <-- no error anymore
  "Published": true
}
A GET request lets you see the correct formatting and all required properties.
A POST must include all properties. Use PATCH for a partial update.

  • stefanmueller gefällt das

Marcus Gesing

Smartstore AG


#5 TripleNico

TripleNico

    Advanced Member

  • Members
  • PunktPunktPunkt
  • 124 Beiträge

Geschrieben: 16 October 2019 - 13:55

A GET request lets you see the correct formatting and all required properties.

 
Thanks for the tip, but the http://yourshop.com/odata/v1/$metadata can also be used for reference right? Because here (also on the swagger page) it describes that Price is of type Decimal but in you example (which now works) Price is of type String, correct? If so, please update the OData metadata.
 
 

A POST must include all properties. Use PATCH for a partial update.

How would one use PATCH if a product doesn't excists yet? And when using POST which fields are required? If i use this:

{
  "Name": "Hello There",
  "FullDescription": "<p>Hello</p>",
  "Sku": "123456",
  "Price": "337.4300",
  "Published": true
}

Then the product looks like this in de webshop backend:

 

admin.catalog.products.producttype.0.labelHello there

 

 

Regarding assignment of manufacturers to products i tried creating products and manufacturers manually and then executing the assignment URL but still no luck. I also experience this with Categories, for example:

http://someshop.com/odata/v1/Products(85)/ProductCategories(636) 
ResponseCode: 422{422}
Content: 
{
  "odata.error": {
    "code": "",
    "message": {
      "lang": "en-US",
      "value": "Exception has been thrown by the target of an invocation."
    }
  }
}


#6 Marcus Gesing

Marcus Gesing

    SmartStore AG

  • Administrators
  • 3799 Beiträge

Geschrieben: 16 October 2019 - 22:05

OData metadata cannot be updated. It's automatically created by OData component. A PATCH (partial update) request of a non existing entity is not possible. Required fields can be retrieved via response of a GET request.


  • stefanmueller gefällt das

Marcus Gesing

Smartstore AG


#7 TripleNico

TripleNico

    Advanced Member

  • Members
  • PunktPunktPunkt
  • 124 Beiträge

Geschrieben: 17 October 2019 - 12:04

OData metadata cannot be updated. It's automatically created by OData component. A PATCH (partial update) request of a non existing entity is not possible.


 
Thanks for confirming that, i was confused because of your previous answer where you said you could also use PATCH instead of POST.
 
 
Could you also answers my questions below?
 

Thanks for the tip, but the http://yourshop.com/odata/v1/$metadata can also be used for reference right? Because here (also on the swagger page) it describes that Price is of type Decimal but in you example (which now works) Price is of type String, correct? If so, please update the OData metadata.


And
 
If i create a new product using POST with the JSON body below, Then the product looks like this in de webshop backend: 

admin.catalog.products.producttype.0.labelHello there

POST Body:

{
  "Name": "Hello There",
  "FullDescription": "<p>Hello</p>",
  "Sku": "123456",
  "Price": "337.4300",
  "Published": true
}

And

 

Regarding assignment of manufacturers to products i tried creating products and manufacturers manually and then executing the assignment URL but still no luck. I also experience this with Categories, for example:
 

http://someshop.com/odata/v1/Products(85)/ProductCategories(636) 
ResponseCode: 422{422}
Content: 
{
  "odata.error": {
    "code": "",
    "message": {
      "lang": "en-US",
      "value": "Exception has been thrown by the target of an invocation."
    }
  }
}


#8 Marcus Gesing

Marcus Gesing

    SmartStore AG

  • Administrators
  • 3799 Beiträge

Geschrieben: 17 October 2019 - 13:15

"but the http://yourshop.com/odata/v1/$metadata can also be used for reference right? "
Yes.
 
"Because here (also on the swagger page) it describes that Price is of type Decimal but in you example (which now works) Price is of type String, correct?"
Yes.
 
"the product looks like this in de webshop backend: admin.catalog.products.producttype.0.labelHello there"
Please provide all required properties, including ProductTypeId. int default of 0 is not a valid value.
 
"Regarding assignment of manufacturers to products i tried creating products and manufacturers manually"
The 422 does not occur with valid IDs.

Marcus Gesing

Smartstore AG



Auch markiert mit einem oder mehrerer dieser Schlüsselwörter: webapi, products, manufacturers