How to upload the image file to the File document using rest API in NodeJS

Hi,

I am trying to upload the image file to the File document using rest API in NodeJS. For that what is the formData example. Kindly let me know. I am getting an error like

<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>

Here is my code:

app.post('/api/getPhoto', upload.single('file'), function (req, res) {
    
    const file = req.file;
    const fileContent = fs.readFileSync(file.path);
    const params = {
    filedata: fileContent.toString('base64'),
    filename: file.filename,
    is_private: 1,
    };

    var options = { 
        method: 'POST',
        url: 'https://pranera.erpnext.com/api/resource/upload_file/',
        headers: 
        { 
            'Authorization': "token "+ "xxxxxxxx" + ":" + "xxxxxxxxxx",
            'Accept': 'application/json' ,
            'Content-Type': 'multipart/form-data'
        },
        formData: params
        };
        
        request(options, function (error, response, body) {  
        if (error) throw new Error(error);
        if (response.statusCode == 200){
            console.log("body",body);
            data = {
               message : "Photo received"
            }
            res.send( {'data': data});
        }
        });
 });

Try again discarding the trailing / character, or pass followAllRedirects: true

Hi @gavindsouza

After removing the trailing / and pass followAllRedirects: true, getting an error like below
{“exception”:“ImportError: Module import failed for upload_file, the DocType you’re trying to open might be deleted.
Error: No module named ‘frappe.core.doctype.upload_file’”,“exc”:“["Traceback (most recent call last):\n File \"apps/frappe/frappe/modules/utils.py\", line 240, in load_doctype_module\n doctype_python_modules[key] = frappe.get_module(module_name)\n File \"apps/frappe/frappe/init.py\", line 1327, in get_module\n return importlib.import_module(modulename)\n File \"/usr/lib/python3.10/importlib/init.py\", line 126, in import_module\n return _bootstrap._gcd_import(name[level:], package, level)\n File \"\", line 1050, in _gcd_import\n File \"\", line 1027, in _find_and_load\n File \"\", line 992, in _find_and_load_unlocked\n File \"\", line 241, in _call_with_frames_removed\n File \"\", line 1050, in _gcd_import\n File \"\", line 1027, in _find_and_load\n File \"\", line 1004, in _find_and_load_unlocked\nModuleNotFoundError: No module named ‘frappe.core.doctype.upload_file’\n\nThe above exception was the direct cause of the following exception:\n\nTraceback (most recent call last):\n File \"apps/frappe/frappe/app.py\", line 66, in application\n response = frappe.api.handle()\n File \"apps/frappe/frappe/api.py\", line 138, in handle\n doc = frappe.get_doc(data).insert()\n File \"apps/frappe/frappe/init.py\", line 1184, in get_doc\n doc = frappe.model.document.get_doc(*args, **kwargs)\n File \"apps/frappe/frappe/model/document.py\", line 72, in get_doc\n controller = get_controller(doctype)\n File \"apps/frappe/frappe/model/base_document.py\", line 81, in get_controller\n site_controllers[doctype] = _get_controller()\n File \"apps/frappe/frappe/model/base_document.py\", line 63, in _get_controller\n module = load_doctype_module(doctype, module_name)\n File \"apps/frappe/frappe/modules/utils.py\", line 244, in load_doctype_module\n raise ImportError(msg) from e\nImportError: Module import failed for upload_file, the DocType you’re trying to open might be deleted.
Error: No module named ‘frappe.core.doctype.upload_file’\n"]”}

Please help me on this. Thanks.