3

I am sending form to backend by using FormData and using multer on backend to recieve the data, it works fine on my web app but on doing the same axios call to submit data in react-native shows that data fields are being received on backend in following form

{ _parts: [ [ 'firstname', 'fahad' ], [ 'firstname', 'RAO' ] ] }

i am doing following call in my web app (which works there) and in my expo app

const testFOrmData = ()=>{
        
        var data = new FormData()
        data.append("firstname","fahad")
        data.append("firstnae","RAO")
        const adata  = {
            firsn:"rew"
        }
axios.post("http://x.x.x.x:xxxx/api/testform",data)
.then((res)=>{
    console.log(res)
})
alert("SANS")
    }

How to receive it in object form such that i dont have to make changes in my existing code in backend as i have so many forms and i have submitted them using form data in my web app and have to do same in mobile app but i dont want to submit without FormData(by sending object in axios)

my backend

router.post("/testform",upload.none(),(req,res)=>{
    console.log(req.body)
    res.json({resp:req.body})
})

UPDATE

Also tried to send with form-data

  const testFOrmData = ()=>{
                
                var data = new FormData()
                data.append("firstname","fahad")
                data.append("firstnae","RAO")
                const adata  = {
                    firsn:"rew"
                }
        axios.post("http://x.x.x.x:xxxx/api/testform",data, headers: {
    'Content-Type': 'multipart/form-data;', 
  }, )
        .then((res)=>{
            console.log(res)
        })
        alert("SANS")
            }
  • 1
    In react-native, I believe you have to manually include the headers from the `FormData` instance. See if [this answer](https://stackoverflow.com/a/68643919/283366) solves it (scroll down to the _"On Axios - NodeJS"_ bit) – Phil Feb 23 '22 at 06:05
  • I tried that too but then req.body received on backend gets empty. Do i have to pass formdata in boundary? – Mohammad Fahad Rao Feb 23 '22 at 06:11
  • I have mentioned everything i tried already in question – Mohammad Fahad Rao Feb 23 '22 at 06:27
  • oh sorry for that, yes it isn't mentioned in question but after visiting the link you mentioned earlier it says that axios has already setup for formdata but i am going to still edit question. – Mohammad Fahad Rao Feb 23 '22 at 06:32

0 Answers0