Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

207
Views
Uploading file from local file system in node giving 400 error

I am trying to upload a file to an API using multipart/form-data.

I am able to successfully do this using insomnia as follows:

enter image description here

Headers are set to Content-Type multipart/form-data and the basic auth is also set.

When I try to replicate this in my code I receive a 400 error.

Here is what I have at the moment:

const URL = 'https://api.app.uploadplace.com/v1/';
const token = 'randomkey';

async function readFile(path: string) {
  return new Promise((resolve, reject) => {
    fs.readFile(path, 'utf-8', function (err, data) {
      if (err) {
        reject(err);
      }

      resolve(data);
    });
  });
}

export const Upload = async (path: string) => {

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

  let file = await readFile(path);

  form.append('file', file);

  const config = {
    headers: {
      'content-type': 'multipart/form-data'
    },
    auth: {
      username: token,
      password: ''
    }
  }

  await axios.post(URL, form, config).then(() => {
    console.log('success')
  }).catch(err => {
    console.log(err);
  })
};

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The problem is that you are appending the file contents as a string to your form data.

Instead, append the file readable stream...

form.append("file", fs.createReadStream(path));

const config = {
  headers: form.getHeaders() // this includes correct mime boundary tokens
  auth: {
    username: token,
    password: ''
  }
}

See https://github.com/axios/axios#form-data

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!