LMSouq
moodle-core Open

moodle api rest call in c#

GA
Galo Ciber
1 month ago
3 views
Problem Description
I'm doing the next api rest call in c#: String token = "e2a35dbfdaee78097c7ba489xxxxxxxx"; MoodleUser user = new MoodleUser(); user.username = WebUtility.UrlEncode("username"); user.password = WebUtility.UrlEncode("the_password"); user.firstname = WebUtility.UrlEncode("Michael"); user.lastname = WebUtility.UrlEncode("York"); user.email = WebUtility.UrlEncode("test@gmail.com"); List<MoodleUser> userList = new List<MoodleUser>(); userList.Add(user); Array arrUsers = userList.ToArray(); String postData = String.Format(@"users[0][username]={0}&users[0][password]={1}&users[0][firstname]={2}&users[0][lastname]={3}&users[0][email]={4}&users[0][preferences][0][type]={5}&users[0][preferences][0][value]={6}", user.username, user.password, user.firstname, user.lastname, user.email, **"auth_forcepasswordchange", "1");** string createRequest = string.Format("http://domain.es/webservice/rest/server.php?wstoken={0}&wsfunction={1}&moodlewsrestformat=json", token, "core_user_create_users"); // Call Moodle REST Service HttpWebRequest req = (HttpWebRequest)WebRequest.Create(createRequest); req.Method = "POST"; req.ContentType = "application/x-www-form-urlencoded"; // Encode the parameters as form data: byte[] formData = UTF8Encoding.UTF8.GetBytes(postData); req.ContentLength = formData.Length; // Write out the form Data to the request: using (Stream post = req.GetRequestStream()) { post.Write(formData, 0, formData.Length); } .... ..... The problem occurs when call the api with the preferences array auth_forcepasswordchange, the result of the errror is "invalid_parameter_exception". When I call without the preferences parameter, works perfectly. Tank you very much.

AI-Generated Solution

Powered by LMSouq AI · GPT-4.1-mini

✓ Solution Ready
Analyzing problem and generating solution…
Was this solution helpful?
Back to Knowledge Base