I am creating a simple API for uploading files from mobile app.
In my routes/api.php
file I have a route defined:
Route::post("/file", 'UploadController@upload');
then in my controller I validate the request:
public function upload(Request $request){
$this->validate($request, [
'name' => 'required',
'file' => 'file',
'type' => 'required|in:sign,photo',
]);
// do something here....
}
When the request is correct (it passes the validation) everything works fine and Laravel returns JSON response.
But if the request does not pass the validation, i.e. the name
field is missing, Laravel returns 302 page and tries to redirect me to login page.
How can I return proper 40X/50X error with JSON message when validation fails, instead of 302 redirect page?
I'm using Laravel 5.3 and Insomnia for testing the API calls.