Error Encountered When Uploading File Using Moodle API Integration in NestJS

Error Encountered When Uploading File Using Moodle API Integration in NestJS

by Peril Talaviya -
Number of replies: 3

My query is regarding the use of the core_file_upload function in Moodle within a NestJS application. I've successfully integrated the core_course_create_courses function, and it's functioning as expected. Now, I'm attempting to upload a file to a course, for which I'm trying to integrate the core_file_upload function.

Here's the API code

  @Post('/upload')
  async uploadFileToMoodle(@Body() params: any): Promise<any> {
      const courseParams = new URLSearchParams({
        wstoken: '2dfc2e7d219295e5d7469562a93b7d94',
        moodlewsrestformat: 'json',
        wsfunction: 'core_files_upload',
        'contextid': params.contextid,
        'component': params.component,
        'filearea': params.filearea,
        'itemid': params.itemid,
        'filepath': params.filepath,
        'filename': params.filename,
      })

      const response = await axios.post(
        `http://localhost/moodle/webservice/rest/server.php?${courseParams.toString()}`
      );

      return response.data;
  }

The filecontent parameter requires a base64 URL, Am I correct?

Here's an example of how I'm passing the data in Postman:

{
    "contextid": 2,
    "component": "user",
    "filearea": "draft",
    "itemid": 1,
    "filepath": "/",
    "filename": "clickHere.png",
    "filecontent": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAB4AAAAOQCAIAAABXWfsZAAAAAXNSR0IArs4c6QAAIABJREFUeJzs3Xd8FHX+x/HvzJa0Ta+QSkIaCS2AVAERiaCAK
}

I'm providing actual base64 URL, this is just example.

I'm encountering the following error:

{
    "exception": "invalid_parameter_exception",
    "errorcode": "invalidparameter",
    "message": "Invalid parameter value detected"
}

Could you please assist me in identifying the cause of this issue?

Also, once the file is successfully uploaded to Moodle, how can I link it to a specific course?

Average of ratings: -
In reply to Peril Talaviya

Re: Error Encountered When Uploading File Using Moodle API Integration in NestJS

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
At a glance, I can't see the 'contextlevel' or 'instanceid' parameters. They're supposed to have default values but not including optional parameters never seems to work for me.
In reply to Howard Miller

Re: Error Encountered When Uploading File Using Moodle API Integration in NestJS

by Peril Talaviya -
Payload
{
"contextid": 1,
"component": "user",
"filearea": "draft",
"itemid": 0,
"filepath": "/",
"filename": "test.txt",
"filecontent": "data:@file/plain;base64,SGVsbG8gd29ybGQ=",
"contextlevel": "course",
"instanceid": 1
}

Even though I am passing contextlevel and instanceid, it is not working. I have attached the payload that I am passing and also attaching the output that I am getting below :-

Output
{
"exception": "moodle_exception",
"errorcode": "nofile",
"message": "File not specified"
}
In reply to Peril Talaviya

Re: Error Encountered When Uploading File Using Moodle API Integration in NestJS

by Bret Miller -
Picture of Particularly helpful Moodlers
The file content is NOT passed as a parameter to the service. It's passed as a file form variable. The upload.php script uses $_FILES to retrieve the file content. Normally you replicate that by use a form field and get the value of it to add to the post formdata.

See https://moodledev.io/docs/4.4/apis/subsystems/external/files for more information. Note how curl is used with the -F parameter to attach a file object to the post data.

"filecontent" is not a valid parameter, hence the invalid parameter exception.