2
votes

how to fix bug laravel validator with Post man send API. Validator return redirect uri root path. it not errors json status 422.

Laravel redirect to root on request validation error .

I have a required field in a Laravel Request Class and when that field is not present in the request, the request is redirected to root '/'.

I am sending the request via Postman.

2

2 Answers

6
votes

Please add Accept: application/json in you header.

3
votes

Laravel redirect validator root url "/" by it check ajax == false. if it return true, it run

If ($ this-> expectsJson ()) {
             Return new JsonResponse ($ errors, 422);
         } 

I solved the problem by adding setup POST MAN in Headers:

I hope it will be useful to the next person when the validator does not work as you expect, you need to set it up like sending ajax to the routes api:

"Key" => "value"

X-Requested-With => XMLHttpRequest

Post Man need config setting some ajax

Otherwise laravel will not be able to return error code 422.

If ($ this-> expectsJson ()) {
             Return new JsonResponse ($ errors, 422);
         }

==> return false if not have "X-Requested-With" : "XMLHttpRequest" in headers POST MAN.

ajax or not ajax active other #.

sorry i do not know english.