You need to add the headers: Accept: application/json for Laravel to send back the JSON validation data.
Mar 16, 2021
9
Level 2
Request class for API
Hi there I wanna use the request class for my API method but whenever a request is sent to my API method, it actually redirects with HTML, The question is I wanna get a response JSON from the validation class to validate my data, What should I do? thanks in advance
Level 5
i created extend for FormRequest
class ApiRequest extends FormRequest
{
protected function failedValidation(Validator $validator)
{
$error = (new ValidationException($validator))->errors();
throw new HttpResponseException(
response()->json(
[
'success' => false,
'error' => $error
], JsonResponse::HTTP_UNPROCESSABLE_ENTITY)
);
parent::failedValidation($validator);
}
}
and extended other Request files like
class OrderCreateRequest extends ApiRequest
1 like
Please or to participate in this conversation.