32

I'm using FormRequest to validate from which is sent in an API call from my smartphone app. So, I want FormRequest alway return json when validation fail.

I saw the following source code of Laravel framework, the default behaviour of FormRequest is return json if reqeust is Ajax or wantJson.

//Illuminate\Foundation\Http\FormRequest class
/**
 * Get the proper failed validation response for the request.
 *
 * @param  array  $errors
 * @return \Symfony\Component\HttpFoundation\Response
 */
public function response(array $errors)
{
    if ($this->ajax() || $this->wantsJson()) {
        return new JsonResponse($errors, 422);
    }

    return $this->redirector->to($this->getRedirectUrl())
                                    ->withInput($this->except($this->dontFlash))
                                    ->withErrors($errors, $this->errorBag);
}

I knew that I can add Accept= application/json in request header. FormRequest will return json. But I want to provide an easier way to request my API by support json in default without setting any header. So, I tried to find some options to force FormRequest response json in Illuminate\Foundation\Http\FormRequest class. But I didn't find any options which are supported in default.

Solution 1 : Overwrite Request Abstract Class

I tried to overwrite my application request abstract class like followings:

<?php

namespace Laravel5Cg\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\JsonResponse;

abstract class Request extends FormRequest
{
    /**
     * Force response json type when validation fails
     * @var bool
     */
    protected $forceJsonResponse = false;

    /**
     * Get the proper failed validation response for the request.
     *
     * @param  array  $errors
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function response(array $errors)
    {
        if ($this->forceJsonResponse || $this->ajax() || $this->wantsJson()) {
            return new JsonResponse($errors, 422);
        }

        return $this->redirector->to($this->getRedirectUrl())
            ->withInput($this->except($this->dontFlash))
            ->withErrors($errors, $this->errorBag);
    }
}

I added protected $forceJsonResponse = false; to setting if we need to force response json or not. And, in each FormRequest which is extends from Request abstract class. I set that option.

Eg: I made an StoreBlogPostRequest and set $forceJsoResponse=true for this FormRequest and make it response json.

<?php

namespace Laravel5Cg\Http\Requests;

use Laravel5Cg\Http\Requests\Request;

class StoreBlogPostRequest extends Request
{

    /**
     * Force response json type when validation fails
     * @var bool
     */

     protected $forceJsonResponse = true;
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'title' => 'required|unique:posts|max:255',
            'body' => 'required',
        ];
    }
}

Solution 2: Add an Middleware and force change request header

I build a middleware like followings:

namespace Laravel5Cg\Http\Middleware;

use Closure;
use Symfony\Component\HttpFoundation\HeaderBag;

class AddJsonAcceptHeader
{
    /**
     * Add Json HTTP_ACCEPT header for an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $request->server->set('HTTP_ACCEPT', 'application/json');
        $request->headers = new HeaderBag($request->server->getHeaders());
        return $next($request);
    }
}

It 's work. But I wonder is this solutions good? And are there any Laravel Way to help me in this situation ?

Chung
  • 947
  • 1
  • 12
  • 22
  • 2
    Hi, just for a suggestion, since your asking for default response type, then why not just by adding a middleware and add request type to json in ur handle method `$request->header('accept', 'application/json'); return $next($request);` with these, you have a place to make further expansion, without always overriding any methods – terry low Jul 20 '15 at 02:37
  • Thanks ! It's a good idea. I think. I'll update this implementation in the question above – Chung Jul 20 '15 at 08:31
  • Sorry. I tried to set $request->header('Accept','application/json'); in a middleware but I found that my request has the default Accept header '*/*', so I couldn't overwrite that Accept header. I didn't set anything in my request. – Chung Jul 20 '15 at 15:06
  • it doesnt matter, whether you have the default accept value set in your header, the middleware value will override it `$request = $request->header('Accept','application/json'); return $next($request); ` im thinking that, the request is not being persisted. – terry low Jul 20 '15 at 15:28
  • I think you can't not assign $request =$request->header('Accept','application/json'); because $request->header('Accept','application/json'); will return string 'application/json'. – Chung Jul 21 '15 at 14:21
  • 1
    I found the way to overwrite request header, We need to set $request->server and rebuild headerBag like followings: $request->server->set('HTTP_ACCEPT', 'application/json'); $request->headers = new HeaderBag($request->server->getHeaders()); – Chung Jul 21 '15 at 15:37
  • Solution #2 is by far the best especially in Laravel 5.2 Middleware Groups. – brenjt Jan 22 '16 at 05:08

6 Answers6

36

It boggles my mind why this is so hard to do in Laravel. In the end, based on your idea to override the Request class, I came up with this.

app/Http/Requests/ApiRequest.php

<?php

namespace App\Http\Requests;


class ApiRequest extends Request
{
    public function wantsJson()
    {
        return true;
    }
}

Then, in every controller just pass \App\Http\Requests\ApiRequest

public function index(ApiRequest $request)

Bouke Versteegh
  • 4,097
  • 1
  • 39
  • 35
  • 4
    Accept: application/json in your header may also help with this. – Squiggs. Dec 12 '16 at 22:49
  • I was coming across this problem while testing a 5.4 app. Simply adding this method to a [form request](https://laravel.com/docs/5.4/validation#form-request-validation) does the trick. – craig_h Aug 05 '17 at 15:29
  • 1
    "It boggles my mind how hard this is"......."here's a super easy way to do it", lol, that made me laugh. +1 – Wesley Smith Sep 23 '17 at 20:10
  • It doesn't work: such request has empty `$request->all()` – Finesse Jan 30 '18 at 08:08
  • This works in the more binary sense of either working or not working, but it's a real lack-luster solution considering how well drafted the question was. – Tarek Adam Apr 21 '18 at 21:53
  • Doesn't work in my Laravel 5.8, still returning 404 when the request header is not 'Application/json'. – T30 Nov 19 '19 at 10:28
33

I know this post is kind of old but I just made a Middleware that replaces the "Accept" header of the request with "application/json". This makes the wantsJson() function return true when used. (This was tested in Laravel 5.2 but I think it works the same in 5.1)

Here's how you implement that :

  1. Create the file app/Http/Middleware/Jsonify.php

    namespace App\Http\Middleware;
    
    use Closure;
    
    class Jsonify
    {
    
        /**
         * Change the Request headers to accept "application/json" first
         * in order to make the wantsJson() function return true
         *
         * @param  \Illuminate\Http\Request  $request
         * @param  \Closure  $next
         * 
         * @return mixed
         */
        public function handle($request, Closure $next)
        {
            $request->headers->set('Accept', 'application/json');
    
            return $next($request);
        }
    }
    
  2. Add the middleware to your $routeMiddleware table of your app/Http/Kernel.php file

    protected $routeMiddleware = [
        'auth'       => \App\Http\Middleware\Authenticate::class,
        'guest'      => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'jsonify'    => \App\Http\Middleware\Jsonify::class
    ];
    
  3. Finally use it in your routes.php as you would with any middleware. In my case it looks like this :

    Route::group(['prefix' => 'api/v1', 'middleware' => ['jsonify']], function() {
        // Routes
    });
    
Winnipass
  • 904
  • 11
  • 22
SimonDepelchin
  • 2,013
  • 22
  • 18
  • I'm having the same issue and thought I'd try this solution. The problem I'm having with this is that by the time my controller method loads my instance of FormRequest implicitly, its not the same request instance from the one modified in the Jsonify middleware, so wantsJson is effectively "reset" to false. – James Feb 12 '16 at 16:04
  • FormRequest extends Request so it should be the same instance, maybe show some code – SimonDepelchin Feb 14 '16 at 13:43
  • @SimonDepelchin This solution is like the Solution 2 which I mentioned in the question. – Chung Feb 17 '16 at 07:11
  • Yes it is, with more details and more in the "Laravel way" imho. – SimonDepelchin Feb 17 '16 at 12:15
  • This solution is better as it returns everything as JSON. If you make an unauthorised request with `ApiRequest` it will give back a 404 html page, however this will return a 401 Unauthorised JSON error. – Ryan Oct 12 '16 at 23:40
  • L5.5: I added `\App\Http\Middleware\Jsonify::class` to the api middleware group in app/Http/Kernel.php so it applies to all api requests. – Mei Gwilym Feb 09 '18 at 13:40
  • I am still getting HTML response for unauthorized requests instead of JSON – Syclone Jun 13 '18 at 20:38
  • The problem with this solution is that you lose the friendly Laravel error page for every other exception: all the Php errors will be shown in json format from now on! – T30 Nov 19 '19 at 10:30
  • That's only the case if you apply the `jsonify` middleware to all your routes. In the solution I create a specific group for the API, so the other routes are not impacted. – SimonDepelchin Nov 19 '19 at 14:20
7

Based on ZeroOne's response, if you're using Form Request validation you can override the failedValidation method to always return json in case of validation failure.

The good thing about this solution, is that you're not overriding all the responses to return json, but just the validation failures. So for all the other Php exceptions you'll still see the friendly Laravel error page.

namespace App\Http\Requests;

use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;
use Symfony\Component\HttpFoundation\Response;

class InventoryRequest extends FormRequest
{
    protected function failedValidation(Validator $validator)
    {
        throw new HttpResponseException(response($validator->errors(), Response::HTTP_UNPROCESSABLE_ENTITY));
    }
}
T30
  • 11,422
  • 7
  • 53
  • 57
3

if your request has either X-Request-With: XMLHttpRequest header or accept content type as application/json FormRequest will automatically return a json response containing the errors with a status of 422.

enter image description here

Afraz Ahmad
  • 5,193
  • 28
  • 38
0

i just override the failedValidation function

protected function failedValidation(Validator $validator)
{
    if ($this->wantsJson()) {
        throw new HttpResponseException(
            Response::error(__('api.validation_error'), 
            $validator->errors(), 
            470, 
            [], 
            new ValidationException)
        );
    }

    parent::failedValidation($validator);
}

So my custom output sample like below:

{
    "error": true,
    "message": "Validation Error",
    "reference": [
        "The device id field is required.",
        "The os version field is required.",
        "The apps version field is required."
    ],
}

BTW Response::error dont exist in laravel. Im using macroable to create new method

 Response::macro('error', function ($msg = 'Something went wrong', $reference = null, $code = 400, array $headers = [], $exception = null) {
      return response()->json(//custom here);
 });
ZeroOne
  • 8,996
  • 4
  • 27
  • 45
0

I came to this solution (Laravel 9):

throw new ValidationException(
    $validator,
    new JsonResponse([
        'errors' => $validator->errors()->messages(),
    ], 422),
);
Vittore Gravano
  • 706
  • 6
  • 22