简体   繁体   中英

Cannot get the request body in the Backend

I created a FormData object and append with two fields audio and email . When i use axios to send a request, i cannot receive request body in the backend.

const email = 'my@gmail';
const url = 'http://';

const sendData = () =>{

 let fd = new FormData();

 const audio = new Blob(audioData, { type: "audio/ogg; codecs=opus" });

 fd.append('audio' , audio);
 fd.append('email', email);

 const resp =  await axios.post(url, fd);
 

Backend side.


// First middleware 

const { User } = require("../models/userSchema");

module.exports = async function (req, res, next) {

    console.log(req.body); // {}

    next();

};

You need to use the 'Content-Type': 'multipart/form-data' ~ header.

const sendData = () =>{
 let fd = new FormData();
 const audio = new Blob(audioData, { type: "audio/ogg; codecs=opus" });
 fd.append('audio' , audio);
 fd.append('email', email);

 const resp =  await axios.post(url, fd, {
   headers: {
     'Content-Type': 'multipart/form-data'
   }
 });
 console.log(resp);

Well, in addition to the above comments, multipart/form-data was required in this case because its used in uploading form data to server in multiple parts because of large file size.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM