1
The PUT/PATCH methods available in the API via the function Route::apiResource('user','UserController');
are those:
GET and POST requests work normally, but in the PUT/PATCH route implementation I get the "Unauthorized" message from the API:
However, when using a REST client as a Postman:
I can get access to the route, so I believe my Axios request is wrong:
let header = {
headers: {
'Content-Type': 'application/json',
'Authorization': `${this.state.tokenType} ${this.state.token}`
}
}
var body = new FormData();
body.append('canAddAdm', this.state.canAddAdm);
body.append('canAddUser', this.state.canAddUser);
body.append('canAddStructures', this.state.canAddStructures);
Axios.put(`${this.state.url}/api/user/{${this.state.selectedOption}}`, header, body)
.then(res => res.data)
.then(result => {
console.log('result :', result);
})
.catch(error => {
console.log('Error when update user: ', error.response);
})
}
So my question is, what would I be doing wrong to be able to access Laravel’s update method via Axios?
It’s probably Cors’s mistake... Before the request itself the browser sends an OPTIONS request, you need to accept this type of request on the route street in the backend.
– edson alves