title: How to force FormRequest return json in Laravel 5.1?
body: <p>I'm using  <a href="http://laravel.com/docs/5.1/validation#form-request-validation">FormRequest</a> to validate from which is sent in an API call from my smartphone app. So, I want FormRequest alway return json when validation fail.</p>
  
  <p>I saw the following source code of Laravel framework, the default behaviour of FormRequest is return json if reqeust is Ajax or wantJson.</p>
  
  <pre><code>//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-&gt;ajax() || $this-&gt;wantsJson()) {
          return new JsonResponse($errors, 422);
      }
  
      return $this-&gt;redirector-&gt;to($this-&gt;getRedirectUrl())
                                      -&gt;withInput($this-&gt;except($this-&gt;dontFlash))
                                      -&gt;withErrors($errors, $this-&gt;errorBag);
  }
  </code></pre>
  
  <p>I knew that I can add <code>Accept= application/json</code> 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 <code>Illuminate\Foundation\Http\FormRequest</code> class. But I didn't find any options which are supported in default. </p>
  
  <h2>Solution 1 : Overwrite Request Abstract Class</h2>
  
  <p>I tried to overwrite my application request abstract class like followings:</p>
  
  <pre><code>&lt;?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-&gt;forceJsonResponse || $this-&gt;ajax() || $this-&gt;wantsJson()) {
              return new JsonResponse($errors, 422);
          }
  
          return $this-&gt;redirector-&gt;to($this-&gt;getRedirectUrl())
              -&gt;withInput($this-&gt;except($this-&gt;dontFlash))
              -&gt;withErrors($errors, $this-&gt;errorBag);
      }
  }
  </code></pre>
  
  <p>I added <code>protected $forceJsonResponse = false;</code> 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. </p>
  
  <p>Eg:  I made an StoreBlogPostRequest and set <code>$forceJsoResponse=true</code> for this FormRequest and make it response json.</p>
  
  <pre><code>&lt;?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' =&gt; 'required|unique:posts|max:255',
              'body' =&gt; 'required',
          ];
      }
  }
  </code></pre>
  
  <h2>Solution 2: Add an Middleware and force change request header</h2>
  
  <p>I build a middleware like followings:
      
  
  <pre><code>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-&gt;server-&gt;set('HTTP_ACCEPT', 'application/json');
          $request-&gt;headers = new HeaderBag($request-&gt;server-&gt;getHeaders());
          return $next($request);
      }
  }
  </code></pre>
  
  <p>It 's work. But I wonder is this solutions good? And are there any Laravel Way to help me in this situation ? </p>