I hava a task to create Moodle courses categories from my c# project, but the following code always return an error response : "Missing required key in single structure: categories" My code is as follows:
string sitename = "http://localhost/moodle";
string token = "0d31f83da49cc21f8dc599fab4b299f0";
string url = sitename + "/webservice/rest/server.php?wstoken=" + token + "&wsfunction=core_course_create_categories&moodlewsrestformat=json";
category cat = new category();
List<category> categories = new List<category>();
cat.name = HttpUtility.UrlEncode("2019");
cat.parent = int.Parse(HttpUtility.UrlEncode("2"));
cat.idnumber = HttpUtility.UrlEncode("11");
cat.description = HttpUtility.UrlEncode("Academic year 2019");
cat.descriptionformat = int.Parse(HttpUtility.UrlEncode("1"));
cat.theme = HttpUtility.UrlEncode("");
categories.Add(cat);
Dictionary<string, List<category>> param = new Dictionary<string, List<category>>();
param.Add("categories", categories.ToList());
string jsonList = JsonConvert.SerializeObject(param.ToArray(), Formatting.None);
var client = new RestClient(url);
var request = new RestRequest();
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Accept", "application/json");
request.RequestFormat = DataFormat.Json;
request.AddJsonBody(jsonList);
request.Method = Method.POST;
var response = client.Execute(request);
//category class code
public class category
{
public string name { get; set; }
public int parent { get; set; }
public string idnumber { get; set; }
public string description { get; set; }
public int descriptionformat { get; set; }
public string theme { get; set; }
public category()
{
descriptionformat = 1;
theme = "";
}
}
Aucun commentaire:
Enregistrer un commentaire