I am using default Request for a public method in controller, when I use validation required and don’t pass the required params in api payload with postman , it just redirect me to login page and does not show any validate rule message.
my method :
class OrderController extends Controller
{
public function createOrder(Request $request)
{
$this->validate($request,[
'name' => 'required',
'phone' => 'required',
'address' => 'required',
'wallet_id' => 'required'
]);
return 'something';
}
}
with this route:
Route::post('/createOrder', 'OrderController@createOrder')->middleware('auth:api');
in postman (or application) when I don’t send one of required parameters, it redirect me to login page.
thanks in advance
2
Answers
I found it, just need to create a custom request and make method
failedValidation
in the custom request and returnHttpResponseException
like below:and in my controller implement the custom request:
In Postman, in your request, in Headers tab set
Accept
key andapplication/json
as value. In this way your Authenticate middleware should avoid redirection in case you are not authenticated.The default Authenticate middleware: