I'm using the Moodle REST webservice in combination with Qt. Login works, other functions with single parameters are working too.
Now I want to use the function "core_user_update_users" which takes an array as parameter.
The official documentation says:
General Structure:
list of (
object {
id int //ID of the user
username string Optional //Username policy is defined in Moodle security config.
... //more optional strings
customfields Optional //User custom fields (also known as user profil fields)
list of (
object {
type string //The name of the custom field
value string //The value of the custom field
}
)preferences Optional //User preferences
list of (
object {
type string //The name of the preference
value string //The value of the preference
}
)}
)
and:
REST (POST parameters)
users[0][id]= int
users[0][username]= string //optional
//many more parameters, but all optional like above
So I thought this would be my HTTP Request:
auto NetworkPostRequest = QNetworkRequest(QUrl("http://XXX.XXX.XXX.XXX/webservice/rest/server.php?wstoken=XXXXXXXXX&moodlewsrestformat=json&wsfunction=core_user_update_users" ));
NetworkPostRequest.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
QByteArray Data;
Data.append("users[0][id]=12");
Data.append("&users[0][firstname]=QtTestname");
this->NetworkAccessManager->put(NetworkPostRequest, Data);
But I get the following debugmessage from moodle:
{
"debuginfo": "Missing required key in single structure: users",
"errorcode": "invalidparameter",
"exception": "invalid_parameter_exception",
"message": "Invalid parameter value detected"
}
Obviously there is something wrong with Data, but I can't figure it out. Hopefully someone here can help.
The Solution was to add an "&" to the first append of Data.
QByteArray Data;
Data.append("&users[0][id]=12");
Data.append("&users[0][firstname]=QtTestname");
this->NetworkAccessManager->put(NetworkPostRequest, Data);
Moodle functions without an array as argument don't care about that, so it doesn't got my attention until now.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With