I develop api, I use the built-in validation Request. In case of successful validation, json is returned (everything is fine), in case of not successful validation, the html page is returned. in the header I submit application / json. How to return an error in json format? Who can already faced? I can’t stupidly find the docks, everything is ok with agiaks, the error is returned to json with the status (422) (sort of), but the usual request through curl doesn’t work out

  • And how do you return an error? In what form is the error returned - just an inscription about a 422 error or a Laravel stub / stack? - Artem Korsunov
  • Laravelskaya stub - Denis Kisel

3 answers 3

Do you use the Accept: application/json header?

Try creating a wantJson method that returns true:

 namespace App\Http\Requests; class ApiRequest extends Request { public function wantsJson() { return true; } } 
  • The all () and post () methods did not return anything during inheritance - Denis Kissel

All Exceptions are processed in Exceptions / Handler.php there and you need to check! Here is my API handler:

 /** * Render an exception into an HTTP response. * * @param \Illuminate\Http\Request $request * @param \Exception $exception * @return \Illuminate\Http\Response */ public function render($request, Exception $exception) { return $this->checkValidationException($request, $exception); } /** * Check for validation exception. * * @param \Illuminate\Http\Request $request * @param \Exception $e * * @return mixed */ private function checkValidationException($request, $e) { if ($e instanceof ValidationException && $request->is(RouteServiceProvider::SSO_PATTERN)) { return response()->json([ 'code' => $e->status, 'message' => $e->getMessage(), 'errors' => $e->errors() ], $e->status); } elseif ($e instanceof TokenExpiredException) { return response()->json(['message' => 'Token is Expired'], Response::HTTP_UNAUTHORIZED); } elseif ($e instanceof TokenInvalidException) { return response()->json(['message' => 'Token is Invalid'], Response::HTTP_UNAUTHORIZED); } elseif ($e instanceof JWTException) { return response()->json(['message' => 'Unauthenticated.'], Response::HTTP_UNAUTHORIZED); } return return parent::render($request, $e); } 

    I do not know how correct this decision is (I guess what can be done more gracefully), but so far I have reached such a decision: I made a Request wrapper.

     namespace App\Http\Requests\Api; use Illuminate\Http\Request; use Illuminate\Support\Facades\Validator; class ApiRequest { protected $request = null; /** * Determine if the user is authorized to make this request. * * @return bool */ public function __construct(Request $request) { $this->request = $request; } public function __call($name, $arguments) { if (method_exists($this->request, $name)) { return call_user_func_array([$this->request, $name], $arguments); } } public function __get($name) { if (property_exists($this->request, $name)) { return $this->request->{$name}; } } public function __set($name, $value) { if (property_exists($this->request, $name)) { return $this->request->{$name} = $value; } } public function validateWithErrors() { $output = []; $validator = Validator::make($this->request->all(), $this->rules(), $this->messages()); if ($validator->fails()) { $output = $validator->messages(); } return $output; } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ 'email' => 'required', 'http_server' => 'required|string', 'oc_version' => 'required|min:7|max:14', 'extension_code' => 'required|string' ]; } public function messages() { return [ 'email.required' => 'Custom error', 'http_server.required' => 'Custom server' ]; } } 

    In the client code:

     $output['errors'] = $request->validateWithErrors();