1

Trying to create a user in moodle but all I'm getting is <?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<EXCEPTION class=\"moodle_exception\">\n<ERRORCODE>invalidtoken</ERRORCODE>\n<MESSAGE>Invalid token - token not found</MESSAGE>\n</EXCEPTION>\n

The token is absolutely correct because I'm getting it from /admin/settings.php?section=webservicetokens

This is the code I'm using

const axios = require('axios');

var token = 'XXXXXXXXXXXXXxx';
var functionname = 'core_user_create_users';

var userstocreate = [{
    username: 'bananaicecream',
    password: '1234',
    firstname: 'Banana',
    lastname: 'Republic',
    email: 'test@example.com',
    idnumber: 'AUTOGENERATEDID001',
    lang: 'en',
    description: 'If you die you die',
    country: 'us'
}];

export async function callMoodle() {
    return axios({
        url: `https://somedomainname.moodlecloud.com/webservice/rest/server.php`,
        method: 'post',
        data: {
            wstoken: token,
            wsfunction: functionname,
            moodlewsrestformat: 'json',
            users: userstocreate
        }
    })
    .then( (response) => {
        return {status: response.status, response: response.data};
    })
    .catch( (error) => {
        return {status: error.response.status, response: error.response.data};
    });
}

Any clue what I'm doing wrong? because i have set up an external service called 'My Service' and assigned the appropriate functions for it as well

enter image description here

Below is the functions

enter image description here

Shan
  • 948
  • 1
  • 9
  • 17

2 Answers2

2

SOLVED - You need to send the data as form data

const axios = require('axios');
const FormData = require('form-data');

export async function callMoodle() {
    const formData = new FormData();
    await formData.append('moodlewsrestformat', 'json');
    await formData.append('wsfunction', 'core_user_create_users');
    await formData.append('wstoken', 'TOKEN_HERE');
    await formData.append('users[0][username]', 'bananaicecream');
    await formData.append('users[0][password]', 'xxxxxxxx');
    await formData.append('users[0][firstname]', 'Banana');
    await formData.append('users[0][lastname]', 'Republic');
    await formData.append('users[0][email]', 'user@example.com');
    await formData.append('users[0][idnumber]', 'AUTOGENERATEDID001');
    await formData.append('users[0][lang]', 'en');
    await formData.append('users[0][description]', 'If you die you die');

    return axios.post('https://somedomain.moodlecloud.com/webservice/rest/server.php', formData, {
        headers: formData.getHeaders()
    })
    .then( (response) => {
        let response_object = {status: response.status, response: response.data};
        return response_object;
    })
    .catch( (error) => {
        let response_object = {status: error.response.status, response: error.response.data};
        return response_object;
    });
}
Shan
  • 948
  • 1
  • 9
  • 17
0

I would recommend looking at testing your webservice call via something like postman. This will at least make sure that the parameters and webservice call is correct and indicate if the issue is with your nodejs code or the webservice call.