I'm trying to send a Axios PATCH request to Laravel 5.6 api. My request contains a FormData.
Laravel's api endpoint doesn't read any of the sent data.
ReactJS code
let data = new FormData();
data.append("photo", this.state.photo);
// append another data ....
const headers = {
'Content-Type': 'multipart/form-data',
'enctype' : 'multipart/form-data',
'Authorization' : 'Bearer ' + token
}
axios({
method : "PATCH",
baseURL: this.baseURL,
url : url,
params : params,
data : data,
headers: headers,
}).then(response => {
return response
})
Laravel patch request
public function update(Request $request, $planId)
{
$data = $request->all();
dd($data);
}
Laravel request prints an empty array []
.
Sad but true, when requesting from the browser it happens that Laravel doesn't properly answer to PATCH or PUT requests.
A quick solution might be using a POST
and adding _method: PATCH
as post parameter.
Please try with this updated code
let data = new FormData();
data.append("_method", 'PATCH');
data.append("photo", this.state.photo);
// append another data ....
const headers = {
'Content-Type': 'multipart/form-data',
'enctype' : 'multipart/form-data',
'Authorization' : 'Bearer ' + token
}
axios({
method : "POST",
baseURL: this.baseURL,
url : url,
params : params,
data : data,
headers: headers,
}).then(response => {
return response
})
Another example of the same issue can be found in axios.patch / axios.put is not working in Vue and Laravel
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With