moodle api rest call in c# fails in core_user_create_users with preferences array

moodle api rest call in c# fails in core_user_create_users with preferences array

by Juan Souto -
Number of replies: 1

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.


Average of ratings: -
In reply to Juan Souto

Re: moodle api rest call in c# fails in core_user_create_users with preferences array

by Harmen Stoopendaal -

Change te preferences  value "1" into 1 (it must be an integer value)


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);