0

I am trying to send FormData from my react front-end to my express backend. The formdata contains some key:value pairs for registration and an Image file, The problem is that the server isn't able to pick up the formdata at all, The req.body is just and empty object and due to that the upload isn't working as well. I tried logging the formdata in the frontend and the formdata is fine, it does contains the values and the file but it doesn't reach the server.

My function code from where I am sending the request.

const handleFormSubmit = async (values, action, acceptedFile) => {
        // action.resetForm()
        // here values = {
        //     name:"Some name",
        //     email:"some@gmail.com",
        //     password:"password",
        //     confirm_password:"confirmed_pass"
        // } 
        const formData = new FormData()
        Object.keys(values).map(key=>{
            formData.append(key, values[key])
        })
        formData.append('picture', acceptedFile)
        const raw = await fetch("https://6025-103-79-250-41.ngrok-free.app/check",{
            method:"POST",
            headers:{
                "Content-Type":"multipart/form-data"
            },
            body:formData
        })

        const data = await raw.json()
        console.log(data)
    }

I am using ngrok for port forwarding. Form data here is fine, I logged it and checked it, It contains the file and the values. I am using react-dropzone to get the file and the dropzone exists in a .

My express server code

const express = require('express')
const multer = require('multer')
const cors = require('cors')
const bodyParser = require('body-parser')

const app = express()
app.use(cors())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())

const storage = multer.diskStorage({
    destination: (req, file, cb)=> {
        cb(null, 'Images/')
    },
    filename: (req, file, cb)=> {
        cb(null, Date.now() + "_" + file.originalname)
    }
})

const uploadMiddleWare = multer({ storage: storage })

app.post("/check", uploadMiddleWare.single('picture'), (req, res)=>{
    console.log(req.body)
    // req.body logs as an empty object {}
    res.status(200).json(req.body)
})

app.listen(3001,()=>{
    console.log("Server started")
})
Silenx
  • 37
  • 6
  • 1
    Remove the request headers, [you don't need them and they're wrong anyway](https://stackoverflow.com/a/68643919/283366) – Phil Jun 07 '23 at 04:28
  • Thanks that did worked! So while I send formdata the runtime already knows what headers to put, Just a question if my headers were same as what's being put then why it didn't work (I re-edited this comment to match replies) – Silenx Jun 07 '23 at 04:36
  • Because they aren't the same. See the linked answer regarding [mime boundary tokens](https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html) – Phil Jun 07 '23 at 04:37
  • Got it, thank you! – Silenx Jun 07 '23 at 04:39
  • Btw can you write it as an answer so I can accept it? – Silenx Jun 07 '23 at 11:02

1 Answers1

1

The backend part is working fine. Can you try to check request body?

Sar Li Ram
  • 46
  • 1
  • 4
  • Someone already answered me in the comments but didn't make it an answer so i couldn't mark it. The problem was with the request body, thank you for the help! – Silenx Jun 07 '23 at 11:09