Api upload file to Note

Hey, I’m trying to create a note (via Api) with an attachment, but the file’s content is nowhere to be saved. The file name is visible in the note but returns “File corrupted” when you try to open it

playload:


{
  "data": {
    "type": "Notes",
    "id": "c75a5ec4-965d-4dba-92bd-55f9e88d4e8d",
    "attributes": {
      "name": "testapi5",
      "filename": "test.txt",
      "file_mime_type": "text/plain",
      "file": "dGVzdCB0ZXh0"
    }
  }
}

when I use the filename_file attribute, the server returns “Property filename_file in Note module is invalid”

Api V8 SuiteCRM ver. 7.11.2

@LMEC did you find any solution on this issue? I face exactly the same problem now!!

Yes. It is implemented in SOAP but I using REST so i added this code to /modules/Notes/Note.php

public function save(){
    parent::save();
    if ($_SERVER['CONTENT_TYPE']=='application/vnd.api+json' && $this->file){
          $upload_file = new UploadFile('uploadfile');
          $decodedFile = base64_decode($this->file);
          $upload_file->set_for_soap($this->filename, $decodedFile);

          $ext_pos = strrpos($upload_file->stored_file_name, ".");
          $upload_file->file_ext = substr($upload_file->stored_file_name, $ext_pos + 1);
          if (in_array($upload_file->file_ext, $sugar_config['upload_badext'])) {
              $upload_file->stored_file_name .= ".txt";
              $upload_file->file_ext = "txt";
          }
          $upload_file->final_move($this->id);
    }
  }
1 Like

Thank you, as i understand there is implemented in REST way to upload files…
I will try to use your code and see if i manage to resolve my issue.

@LMEC, thank you!!! Your solution worked! I manage after some time to upload documents to Notes

Hi @LMEC I’m trying to achieve the same result with regarding to posting notes using API and I see what you guys have done. What endpoint did you use to achieve the above? Would this ‘{{site_url}}/api/v8/modules/Notes’ endpoint be sufficient? But it is currently giving me errors

If you create or update a note, you must send POST or PATCH at “/Api/V8/module”. If you need more information check this https://docs.suitecrm.com/developer/api/developer-setup-guide/json-api/#_create_a_module_record