Search code examples
node.jsreactjsexpressaxiosmultipartform-data

"Failed to read the requ est form. Missing content-type boundary.". Axios. React. Node, express


I am sending a request from the react to my backend. And from my backend I am already sending a request to another backend(asp .net). I get this error: {"":["Failed to read the requ est form. Missing content-type boundary."]}. All data must be form data

const express = require('express');
const router = express.Router();
const axios = require("axios");
let FormData = require("form-data");
router.post('/proctoring', async (req, res) => {
    let form = new FormData();
    form.append("TestId", 120521);
    const result = await axios("https://dashboard.curs.kz:8023/api/Tests/ProctoringFiles", {
        method: "POST",
        headers: { 'Authorization': req.headers['authorization'], 'Content-Type': 'multipart/form-data' },
        data: JSON.stringify(form)
    }).catch(e => console.log(JSON.stringify(e.response.data), "error"));
    console.log(result);
});

module.exports = router;

if i remove JSON.stringify it's also will not working


Solution

  • Content-Type': 'multipart/form-data'
    data: JSON.stringify(form)
    

    To send multipart data:

    1. Pass a FormData object to data. Do not pass a string.
    2. Do not specify a content type. The browser will generate from the FormData object (and it will include the mandatory boundary parameter.

    To send JSON data:

    1. Set the correct content-type (application/json)
    2. Generate the JSON from a plain object and not a FormData object (which won't stringify)