thearyanahmed
thearyanahmed

Reputation: 606

Axios patch request not working with Laravel

I'm trying to make a patch request from axios, but the data is not being sent.

const url = route('industries::update',id)

const headers = {
    'Content-Type': 'application/x-www-form-urlencoded'
}

return axios.patch(url,data,{ headers })

the data parameter is "FormData()"

I've also tried appending the key method with _PATCH in the form data

let formData = new FormData()
formData.append('method','_PATCH')

But nothing seems to work. I get the default laravel's 422 error from the responses (that the values are required).

Upvotes: 1

Views: 513

Answers (1)

Hafez Divandari
Hafez Divandari

Reputation: 9059

1st. Your data is an instance of FormData but your header is application/x-www-form-urlencoded which is wrong, use multipart/form-data instead. However, it will be set automatically when you use instance of FormData as data.

2nd. Send request via axios.post and append _method: PATCH to your formData:

const url = route('industries::update', id)

/*
const headers = {
    'Content-Type': ' multipart/form-data' // <= instead of `application/x-www-form-urlencoded`
}
*/

return axios.post(url, data) // <= instead of `axios.patch` and omit unnecessary `headers`

And:

let formData = new FormData()
formData.append('_method', 'PATCH') // <= instead of `('method', '_PATCH')`

Upvotes: 3

Related Questions