'TypeError: expected string or bytes-like object' When Uploading Files to ERPNext

Hello here.
I created some custom doctypes in ERPNext which have some fields that are attachments.
I have also created a custom application using React Native (Typescript) and I am trying to upload files through the rest-api.
I am using the /api/method/upload_file endpoint to attach images to the docs, when I try to do this using postman, it works well as shown in the screenshot below.

However, when I do this using axios or fetch Javascript api, it throws an error TypeError: expected string or bytes-like object.
I have tried all ways without success.

const handleUploadFile = async () => {
    try {
      const imageFile = new FormData();
      const imgurl = await getBase64(file);
      imageFile.append("file", file.toString());
      imageFile.append("doctype", "Shipping Order");
      imageFile.append("folder", "Home");
      imageFile.append('fieldname', 'payment_slip')
      imageFile.append("optimize", true);
      imageFile.append("is_private", 1);
      const response = await axios.post("", {
        data: imageFile,
      }, {
        headers: {
          "Content-Type": "Application/json",
            Authorization: "Token 97afa5b2b775ebf33007:bc6e8d4d2e5380f99f4c",
    } catch (error) {

Has anyone been able to implement this, or does anyone spot an error in my implementation.
Thanks in advance.

Did you solve this? I have the same error

Hello @Mortodus , Yes I managed to solve it. I just had to pass the data literally instead of creating a FormData Object.

Here is my implementation.

const data = {
  file: yourFile,
  doctype: "Shipping Order",
  folder: "Home",
  docname: "LOG-SHP-ORD-2023-00007",
  fieldname: "payment_slip",
  is_private: 1

const response = await axios.post("<url>/api/method/upload_file", {
  data: data
}, headers: {...})

This should work, good luck!

1 Like

HI @stuartdambi,
Thanks for the response.
You response did the job.
Thank you!