[Vuejs]-Empty response from post with formData

0👍

You are calling a then after the fetch’s then which does not exists.

You can use arrow functions at then for accessing the response variable you got from the request

let data = new FormData()
data.append('name', 'hey')

fetch('http://homestead.test/api/customers', {
    method: 'POST',
    headers: {
        'Content-type': 'multipart/form-data'
    },
    body: data
})
.then((response) => {
  // now you can access response here
  console.log(response)
})

0👍

I think the problem may be with the content header type it should

{
    Content-type: 'application/json'
}

And also try to send the data with:

body :JSON.stringify(data)

0👍

I do not understand why FormData is empty, but all requests with JSON body works.

let data = new FormData() 
data.append('name', 'hey')

let json = JSON.stringify(Object.fromEntries(data));;
fetch('http://homestead.test/api/customers', {
    method: 'POST',
    headers: {
        'Content-type': 'application/json'
    },
    body: data
})
.then((response) => response.json())
.then((response) => {
    console.log(response)
})

Leave a comment