Hello everybody.
I have problem with add new product with use REST API in C#.
I gice message: "{\"message\":\"%fieldName is a required field.\",\"parameters\":{\"fieldName\":\"product\"}}"
Please hel me.
My code:
public string AddNewProduct()
{
var category = new Product();
category.name = "abc111";
category.sku = "sku";
category.attribute_set_id = 1;
category.price = 234;
var token = GetBearerToken().Trim('"');
var client = new RestClient { BaseUrl = new Uri(_host) };
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization", "Bearer " + token);
request.Resource = "rest/V1/products";
var p = new prod();
p.products = category;
string json = JsonConvert.SerializeObject(p, Formatting.Indented);
request.AddParameter("application/json", json, ParameterType.RequestBody);
var response = client.Execute(request);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
return response.Content;
}
else
{
return "" + response.Content;
}
}
You forgot few parameters , which is required to create product using Rest Api in Magento 2.
Also all the parameters are coming under the product parameters , so below i have posted parameters structure for products.
Body:
{
"product": {
"sku": "sku",
"name": "abc1111",
"price": 234,
"status": 1,
"type_id": "simple",
"attribute_set_id":1
}
}
I added this fields. Still not work.
Update your full structure here , Have you taken this under the product ?
var product = new Product();
product.sku = "sku";
product.name = "abc111";
product.price = 234;
product.status = 1;
product.type_id = "simple";
product.attribute_set_id = 4;
var token = GetBearerToken().Trim('"');
var client = new RestClient { BaseUrl = new Uri(_host) };
var request = new RestRequest(Method.POST);
request.AddHeader("Accept", "application/json");
request.AddHeader("Authorization", "Bearer " + token);
request.Resource = "rest/V1/products";
var prod = new prod();
prod.products = product;
string json = JsonConvert.SerializeObject(prod, Formatting.Indented);
//request.AddBody(json);
request.AddParameter("application/json", json, ParameterType.RequestBody);
var response = client.Execute(request);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
return response.Content;
}
else
{
return " " + response.Content;
}
I am reading Documentation Rest api (swagger). I know field "fieldName" is on error message
Yes thats what i am saying , you are missing main parameter which is product.
Something like below code :
{ // Here you are missing first product parameter, all parameters comes under product
"product": { "sku": "sku", "name": "abc1111", "price": 234, "status": 1, "type_id": "simple", "attribute_set_id":1 } }
If you have seen like FieldName is required field they have also mention - {\"fieldName\":\"product\"}}" so here you are missing main parameter called product , all sku , type_id and other paramters needs to pass under the product.
Tis is my result:
{
"product": {
"entity_id": 0,
"type_id": "simple",
"attribute_set_id": 4,
"fieldName": "A",
"required_options": 0,
"sku": "sku",
"name": "abc111",
"meta_title": null,
"meta_description": null,
"url_key": null,
"custom_design": null,
"page_layout": null,
"options_container": null,
"country_of_manufacture": null,
"msrp_enabled": null,
"msrp_display_actual_price_type": null,
"gift_message_available": null,
"price": 234.0,
"special_price": null,
"weight": 0.0,
"msrp": null,
"status": 1,
"visibility": 0,
"enable_googlecheckout": null,
"tax_class_id": null,
"description": null,
"short_description": null,
"meta_keyword": null,
"custom_layout_update": null,
"special_from_date": null,
"special_to_date": null,
"news_from_date": null,
"news_to_date": null,
"custom_design_from": null,
"custom_design_to": null,
"group_price": null,
"tier_price": null,
"stock_data": null,
"Attributes": null
}
}
And new message with error:
"{\"message\":\"Internal Error. Details are available in Magento log file. Report ID: webapi-5a93c39b2ec65\"}"
okay , it means your previous issue is resolved by adding product and pass all the parameters under the product right ?
Now you have some different error , open your error log file and post the error log you have.
Also check in your magento 2 backend , product is created or not ?