Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send data correct axios Error: Multipart: Boundary not found

I don't know why I receive on server [Error: Multipart: Boundary not found] and bundle.js:37628 POST http://localhost:8800/exporttocsv 500 (Internal Server Error) When I make post through

<form action="/exporttocsv" method="POST"  encType="multipart/form-data">

post works correctly, but through axios doesn't work.

Please help me fix the mistake

this my code /--client

import axios from 'axios'
var formData = new FormData()

const config = { headers: { 'Content-Type': 'multipart/form-data' } };
export const ipmortToCSV = (file) => dispatch => {

formData.append('file',file)
console.log(formData.getAll('data'))

axios.post('/exporttocsv', {
          "UploadCommand": formData
      },config)
      .then(function (response) {
        console.log(response);
      })
      .catch(function (error) {
        console.log(error);
      });

//--server

const router = require('express').Router()
var csv = require('csv-express')
const controllers = require('../../controllers/exporttocsv')
var multer  = require('multer')
var upload = multer({dest : 'exporttocsv/'})

router.get('/', (req, res) => {
      controllers.exportToCsv(req,res)
  })
router.post('/',upload.single('file'),(req,res) => { 
    //controllers.importToCsv(req,res)
})

module.exports = router
like image 314
Roman Stefanko Avatar asked Mar 30 '18 18:03

Roman Stefanko


People also ask

How send multipart form data in react Axios?

querySelector("form"); if (form) { form. addEventListener("submit", (e) => { e. preventDefault(); const formData = new FormData(form); axios . post("/update-profile", formData, { headers: { "Content-Type": "multipart/form-data", }, }) .

How do you add a multipart form data in Axios?

To send multipart form data with Axios, you need to use the FormData class. Browsers have a built-in FormData class, but Node. js doesn't, so you need to use the form-data npm module. To create the form, you must append the data to the form that will be sent to the server using the append() method.

What is multipart form data boundary?

Multipart/form-data is a special type of body that each value is sent as a block of data. Each part is separated by the defined delimiter (a.k.a boundary). The key value is described in the Content-Disposition header of each part. By using multipart/form-data, you can: Send a file or multiple files to your server.


1 Answers

I was struggling with this issue of multipart boundary not found with fetch api calling to a nestjs server. What I tried was to remove the

'Content-Type': 'multipart/form-data',

headers so that Fetch api automatically set the headers and it worked. Try it out

like image 196
Charith Jayasanka Avatar answered Oct 05 '22 21:10

Charith Jayasanka