4

I have used C# to call magento rest API to create a new product (magento version 2.2.5)

this is the body data:

{
  "product": {
  "id": 0,
  "sku": "MS-Champ11",
  "name": "Champ Tee11",
  "attribute_set_id": 9,
  "price": 110,
  "status": 1,
  "visibility": 4,
  "type_id": "simple",
  "created_at": null,
  "updated_at": null,
  "weight": 5,
  "extension_attributes": null,
  "product_links": null,
  "options": null,
  "media_gallery_entries": null,
  "tier_prices": null,
  "custom_attributes": [
      {
        "attribute_code": "description",
        "value": "Test Description"
      }
    ]
  },
  "save_options": false
}

this is my C# code:

public string CreateProduct(Product product)
    {
        try
        {
            var request = CreateRequest("/rest/V1/products", Method.POST, Token);
            var prod = new ProductModel();
            prod.Product = product;
            string json = JsonConvert.SerializeObject(prod, Formatting.Indented);
            request.AddParameter("application/json", json, ParameterType.RequestBody);

            var response = Client.Execute(request);
            if (response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                return "Succeed";
            }
            else
            {
                return "Failed";
            }
        }
        catch (Exception ex)
        {
            return ex.ToString();
        }
    }

I tried many time but it was always responding error "BadRequest"

{"message":"Invalid product data: %1","parameters":["Invalid attribute set entity type"]}

what was i doing wrong? please help me.

2
  • 1
    Please check "attribute_set_id": 9, this 9 is exist in magento.
    – kunj
    Aug 9 '18 at 5:42
  • yahoo. It's the cause of error, thanks very much Aug 9 '18 at 6:32
3
  1. In Magento Every product is assigned to a specific attribute set.
  2. New Attribute set can be created by logging into Magento Admin -> Stores -> Attributes -> Attribute Set and then click Add attribute set button, fill the form and save it.
  3. Information about Attribute Set and attribute_set_id can be found in database table eav_attribute_set.(select attribute_set_id from eav_attribute_set where attribute_set_name = 'Name of the Attribute Set where you want to create product';)
  4. Use that attribute set id in your API request.
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.