Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

iamamirsalehi's avatar

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

0 likes
9 replies
andyabihaidar's avatar

You need to add the headers: Accept: application/json for Laravel to send back the JSON validation data.

1 like
trin's avatar
trin
Best Answer
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
iamamirsalehi's avatar

@trin the namespace of the Validator parameter in the method is Illuminate\Validation\Validator, right?

trin's avatar

yes

<?php
namespace App\Http\Requests;

use Illuminate\Http\JsonResponse;
use Illuminate\Validation\ValidationException;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Foundation\Http\FormRequest;
martinbean's avatar

thank you, this solution is better

@iamamirsalehi It’s not, though. It’s just a workaround for being sloppy when making requests.

If you want JSON back, you need to tell Laravel you want JSON by specifying the Accept like @andyabihaidar says.

2 likes
trin's avatar

controversial statement. what if i send Accept: image/png? )) in my case, i need not only json response, but with structure, like:

// good response
{
	"success": true,
	"data": ...
}

// bad response
{
	"success": false,
	"error": ...
}

and if i create backend api with laravel (no need web response), im not care about Accept, all responses are json. as I understand, i have two ways to realize it

  • extends FormRequest, as i said
  • extends Exceptions/Handle.phpto override render like (for example)
public function render($request, $exception)
    {
        if ($exception instanceof ModelNotFoundException) {
            return response()->json([
                'success' => false,
                'error' => 'not found id in model',
                'exception' => $exception
            ], Response::HTTP_NOT_FOUND);
        }
        return parent::render($request, $exception);
    }

but as I understood, second way more globally and if i use some external module with web interface (telescope etc) it is bad idea.

if i wrong, tell me please best way to response all my requests with json on my structure. hmmm... I'll think about it

Please or to participate in this conversation.