Laravel Request has Empty Files

  Kiến thức lập trình

Stumped on this one.

I have a Laravel endpoint that is expected to receive an uploaded image file. The request is sent from a UI using javascript fetch. When I run the UI, I see the file being POSTed in the network tab:

But when the request is received by Laravel, there are no files. The dd prints out:

When I Log the request, it’s empty. For reference, the rest of the Laravel endpoints are working as expected. Also, I have tried with and without content-type of multipart/form-data. No difference either way.

The function in Laravel is super simple right now, it’s just not receiving anything:

public function imageUpload(Request $request){
            $file = $request->file('upload')->store('storage');
        }catch(Exception $e){
            return parent::respondWithError("Couldn't store image file.");
        return response()->json([
            "status" => true,
            "message" => "File successfully uploaded",
            "file" => $file
        return parent::respondWithError("No file present: ".json_encode($request));

The JavaScript in for the UI looks like this:

const data = new FormData();
        data.append('upload', file);

        let response = fetch(api_url, {
            method: 'post',
            headers: {
            'Content-Type': 'multipart/form-data',
            'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute('content'),
            'Accept': 'application/json'
            body: data
          .then(data => {
            return data;

I’ve spent hours on this. Here are a few things I’ve ruled out:

  1. I initially thought it was Cloudflare stripping out the file, but after bypassing Cloudflare it’s the same problem.
  2. I’ve tried different Content-Type headers
  3. I’ve tried encoding the image as form data vs not

I’ve hit a wall. Anyone have any ideas?



The issue was resolved by removing the Content-Type from the header of the request (though I swear I tried that previously). The request now treats the image as binary in form data and this seems to be working with Laravel’s Request handler.