Can't Post Files using Request or Axios in node js

Viewed 666

I have to take data from my form in my front end code and upload the data to another server using API calls, which I am unable to do right now. I have tried numerous ways and tricks mentioned over the internet to solve the problem faced while uploading a file in NodeJs using Request and Axios but none of them are working for just this particular case and I don't know why.

Here is the code in my client-side if we use Axios:

app.post('/job/addjob', upload.any(), (req, res) => {
    console.log(req.files[0].path);
    var form = new formData();
    form.append('Title', req.body.Title);
    // form.append('Services', JSON.stringify(req.body.Services));
    form.append('Startdate', req.body.Startdate);
    form.append('NoOfPositions', req.body.NoOfPositions);
    form.append('EndDate', req.body.EndDate);
    form.append('Experience', req.body.Experience);
    form.append('Rate', req.body.Rate);
    form.append('Location', req.body.Location);
    form.append('Description', req.body.Description);
    // form.append('Questions', JSON.stringify(req.body.Questions));
    form.append('Dresscode', fs.createReadStream(req.files[0].path));
    axios({
        method: 'post',
        url: 'http://13.127.239.92:3000/job/addjob',
        data: form,
        headers: {'Content-Type': 'multipart/form-data', 'Authorization': req.session.verifiedToken}
    }).then(function (response) {
        console.log(response.data);
        res.redirect('/provider');
    })
    .catch(function (err) {
        //handle error
        console.log(err);
        res.redirect('/profile/getProfile');
    });
});

for which the error that I face is

Error Uploading Image.

And here is the code in my client-side if we use Request:

app.post('/job/addjob', upload.any(), (req, res) => {
    console.log(req.files);
    var options = {
      'method': 'POST',
      'url': 'http://13.127.239.92:3000/job/addJob',
      'headers': {
        'Content-Type': 'multipart/form-data',
        'Authorization': req.session.verifiedToken
      },
      formData: {
        'Title': req.body.Title,
    // form.append('Services', JSON.stringify(req.body.Services));
        'Startdate': req.body.Startdate,
        'NoOfPositions': req.body.NoOfPositions,
        'EndDate': req.body.EndDate,
        'Experience': req.body.Experience,
        'Rate': req.body.Rate,
        'Location': req.body.Location,
        'Description': req.body.Description,
    // form.append('Questions', JSON.stringify(req.body.Questions));
        'Dresscode1': fs.createReadStream(req.files[0].path),
      }
    };
    
    request(options, function (error, response) {
        if (error) { console.log(error) }
        else {
            console.log(response.data);
            res.redirect('/provider');
        }
    });
});

for which the error that I face is:

{ Error: socket hang up
    at createHangUpError (_http_client.js:323:15)
    at Socket.socketOnEnd (_http_client.js:426:23)
    at Socket.emit (events.js:203:15)
    at endReadableNT (_stream_readable.js:1145:12)
    at process._tickCallback (internal/process/next_tick.js:63:19) code: 'ECONNRESET' }

This is the fraction of code from the server-side which might come in handy understanding my problem:

    var storage = multer.diskStorage({
        destination: function (req, file, cb) {
          cb(null, 'dresscodes/')
         
         
        },
        filename: function (req, file, cb) {
            let uuid=v4();
          cb(null, uuid +"."+mime.getExtension(file.mimetype))
          pp= uuid +"."+mime.getExtension(file.mimetype);
        }
      });

      let upload = multer({ storage: storage }).any();
      upload(req,res,function(err) {
        if(err) {
            console.log(err);
            return res.end("Error uploading image.");
        } else{
            var fullUrl = req.protocol + '://' + req.get('host') ;

Now to the worst part, I can only make changes to my client-side code and not to the server-side code

0 Answers
Related