Hello i am trying to create a category using the Rest Api, but i am getting an error
Here is my code
Main
static void Main(string[] args) { CreateCategory(); }
public static void CreateCategory()
{
var magento = new Magento(ConfigurationManager.AppSettings["userName"], ConfigurationManager.AppSettings["passWord"]);
magento.CreateCategory("Man");
}
Class Function
private RestRequest CreateRequest(string endPoint, Method method, string token)
{
var request = new RestRequest(endPoint, method);
request.RequestFormat = DataFormat.Json;
request.AddHeader("Authorization", "Bearer " + token);
request.AddHeader("Accept", "application/json");
return request;
}
public void CreateCategory(string name) { var request = CreateRequest("/rest/V1/categories", Method.POST, this.token); var category = new CategoryPost(); var categoryMagento = new CategoryPostMagento(); categoryMagento.name = name; categoryMagento.is_active = true; category.category = categoryMagento; string json = JsonConvert.SerializeObject(category, Formatting.Indented); request.AddParameter("application/json", json, ParameterType.RequestBody); var response = Client.Execute(request); if (response.StatusCode == System.Net.HttpStatusCode.OK) { CategoryPost categories = JsonConvert.DeserializeObject<CategoryPost>(response.Content); //Json cargar a clase } else { } }
My object to hold the category to parsed into json object
public class CategoryPostExtensionAttributes { } public class CategoryPostCustomAttribute { [JsonProperty("attribute_code")] public string attribute_code { get; set; } [JsonProperty("value")] public string value { get; set; } } public class CategoryPostMagento { [JsonProperty("id")] public int id { get; set; } [JsonProperty("parent_id")] public int parent_id { get; set; } [JsonProperty("name")] public string name { get; set; } [JsonProperty("is_active")] public bool is_active { get; set; } [JsonProperty("position")] public int position { get; set; } [JsonProperty("level")] public int level { get; set; } [JsonProperty("children")] public string children { get; set; } [JsonProperty("created_at")] public string created_at { get; set; } [JsonProperty("updated_at")] public string updated_at { get; set; } [JsonProperty("path")] public string path { get; set; } [JsonProperty("available_sort_by")] public IList<string> available_sort_by { get; set; } [JsonProperty("include_in_menu")] public bool include_in_menu { get; set; } [JsonProperty("extension_attributes")] public CategoryPostExtensionAttributes extension_attributes { get; set; } [JsonProperty("custom_attributes")] public IList<CategoryPostCustomAttribute> custom_attributes { get; set; } } public class CategoryPost { [JsonProperty("category")] public CategoryPostMagento category { get; set; } }
And the json file taken from the website of the devdocs
{ "category": { "id": 0, "parent_id": 0, "name": "string", "is_active": true, "position": 0, "level": 0, "children": "string", "created_at": "string", "updated_at": "string", "path": "string", "available_sort_by": [ "string" ], "include_in_menu": true, "extension_attributes": {}, "custom_attributes": [ { "attribute_code": "string", "value": "string" } ] } }
I am getting the respose as folows
message: "Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\magento\lib\internal\Magento\Framework\Webapi\ServiceInputProcessor.php on line 306"
trace: "#0 C:\xampp\htdocs\magento\lib\internal\Magento\Framework\Webapi\ServiceInputProcessor.php(306): Magento\Framework\App\ErrorHandler->handler(2, 'Invalid argumen...', 'C:\\xampp\\htdocs...', 306, Array)
#1 C:\xampp\htdocs\magento\lib\internal\Magento\Framework\Webapi\ServiceInputProcessor.php(275): Magento\Framework\Webapi\ServiceInputProcessor->convertCustomAttributeValue(NULL, 'Magento\\Catalog...')
#2 C:\xampp\htdocs\magento\lib\internal\Magento\Framework\Webapi\ServiceInputProcessor.php(478): Magento\Framework\Webapi\ServiceInputProcessor->_createFromArray('Magento\\Catalog...', Array)
#3 C:\xampp\htdocs\magento\lib\internal\Magento\Framework\Webapi\ServiceInputProcessor.php(165): Magento\Framework\Webapi\ServiceInputProcessor->convertValue(Array, 'Magento\\Catalog...')
#4 C:\xampp\htdocs\magento\app\code\Magento\Webapi\Controller\Rest\InputParamsResolver.php(101): Magento\Framework\Webapi\ServiceInputProcessor->process('Magento\\Catalog...', 'save', Array)
#5 C:\xampp\htdocs\magento\lib\internal\Magento\Framework\Interception\Interceptor.php(58): Magento\Webapi\Controller\Rest\InputParamsResolver->resolve()
#6 C:\xampp\htdocs\magento\lib\internal\Magento\Framework\Interception\Interceptor.php(138): Magento\Webapi\Controller\Rest\InputParamsResolver\Interceptor->___callParent('resolve', Array)
#7 C:\xampp\htdocs\magento\lib\internal\Magento\Framework\Interception\Interceptor.php(153): Magento\Webapi\Controller\Rest\InputParamsResolver\Interceptor->Magento\Framework\Interception\{closure}()
#8 C:\xampp\htdocs\magento\generated\code\Magento\Webapi\Controller\Rest\InputParamsResolver\Interceptor.php(26): Magento\Webapi\Controller\Rest\InputParamsResolver\Interceptor->___callPlugins('resolve', Array, Array)
#9 C:\xampp\htdocs\magento\app\code\Magento\Webapi\Controller\Rest\SynchronousRequestProcessor.php(85): Magento\Webapi\Controller\Rest\InputParamsResolver\Interceptor->resolve()
#10 C:\xampp\htdocs\magento\app\code\Magento\Webapi\Controller\Rest.php(188): Magento\Webapi\Controller\Rest\SynchronousRequestProcessor->process(Object(Magento\Framework\Webapi\Rest\Request\Proxy))
#11 C:\xampp\htdocs\magento\lib\internal\Magento\Framework\Interception\Interceptor.php(58): Magento\Webapi\Controller\Rest->dispatch(Object(Magento\Framework\App\Request\Http))
#12 C:\xampp\htdocs\magento\lib\internal\Magento\Framework\Interception\Interceptor.php(138): Magento\Webapi\Controller\Rest\Interceptor->___callParent('dispatch', Array)
#13 C:\xampp\htdocs\magento\lib\internal\Magento\Framework\Interception\Interceptor.php(153): Magento\Webapi\Controller\Rest\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#14 C:\xampp\htdocs\magento\generated\code\Magento\Webapi\Controller\Rest\Interceptor.php(26): Magento\Webapi\Controller\Rest\Interceptor->___callPlugins('dispatch', Array, Array)
#15 C:\xampp\htdocs\magento\lib\internal\Magento\Framework\App\Http.php(116): Magento\Webapi\Controller\Rest\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
#16 C:\xampp\htdocs\magento\generated\code\Magento\Framework\App\Http\Interceptor.php(24): Magento\Framework\App\Http->launch()
#17 C:\xampp\htdocs\magento\lib\internal\Magento\Framework\App\Bootstrap.php(261): Magento\Framework\App\Http\Interceptor->launch()
#18 C:\xampp\htdocs\magento\index.php(39): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http\Interceptor))
#19 {main}"
Like the values number for the forech cicle in php are not valid i suppose