adavie's avatar

422 Error on uploading file - rejection from JsonMiddleware

I'm using Vuejs and the Element-UI framework, which is outstanding. One a seprate server, I have the Laravel backend, also outstanding! :-)

However, I'm running into a very, very frustrating problem: when I submit the file to my backend, I keep getting a 422 error, with the message: "you did not send the required json in the body"

The request header has the "file" property etc.

In the return function that catches upload errors, I can see that the file was recieved and processed BEFORE it gets to my controller, where it never arrives, because the middleware gets all huffy because there is no Form Payload data attached.

I've tried everything I can think of:

  1. Trying to "modify" the request on Laravel in my custom Request object (Can't seem to do that)
  2. Using el-upload's :data property- which sends the data as params with the request but not as form payloads.
  3. Pulling my hair out and gnashing of teeth,

I could really use some help here.

Laravel config:

Route: Route::post('/upload/site/{siteId}', 'AttachmentsController@siteUpload')->name('upload.sites');

Controller:

    public function siteUpload (AttachmentRequest $request, $site_id)
        {
    //    $this->request->file()
    //    $attach = \App\Attachments::firstOrNew(['site_id' => $site_id]);

    // Never see this message in the logs
       Log::info ('AttachmentController::siteUpload for site [' . $site_id . '] | request = ' . $request) ;

         if ($request->hasFile('file')) {
            $path = $request->file('file')->store('images');
            return response()->json([
               'message' => 'Upload success.',
               'path' => $path,
               'status' => 200
          ], 200);
         }
       }

AttachmentRequest:

<?php

namespace App\Http\Requests;

use Illuminate\Support\Facades\Log;

use Illuminate\Foundation\Http\FormRequest;


class AttachmentRequest extends FormRequest
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        Log::info("I'm in the AttachmentRequest rules()! ") ;   // Never reaches here!
        return ['filename' => 'required', 'file' => 'mimes:pdf, jpg, jpeg, bmp, png|size:5000'] ;
    }

    protected function getValidatorInstance()
    {
      $data = $this->all();
      Log::info("I'm in the AttachmentRequest object! " . $data) ;   // Never reaches here!

      return parent::getValidatorInstance();
    }
}
0 likes
1 reply
adavie's avatar

Well, this is my second question in laracasts and no response, not sure if what I did is a total hack, I've solved it by amending the JsonApiMiddleware file as follows:

  public function handle($request, Closure $next)
    {
        if (in_array($request->getMethod(),$this->methods)) {

            if (json_decode($request->getContent(),true) === null)
            {
            if (!$request->hasFile('file')) {
               return response(['error' =>'you did not send the required json in the body'], 422);
              }
            }
          else {
            $request->merge(json_decode($request->getContent(),true));
          }

        }

        return $next($request);
    }

Please or to participate in this conversation.