Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel PATCH Request doesn't read Axios form data

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 [].

like image 249
Abeer Elhout Avatar asked Dec 05 '22 10:12

Abeer Elhout


1 Answers

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

like image 177
phaberest Avatar answered Dec 10 '22 10:12

phaberest