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

martinszeltins's avatar

How to process formData, getting empty array

Hi, I have this frontend code where I upload an image file:

<input
    @change="changeProfilePicture"
    type="file"
    accept="image/*"
/>

async function changeProfilePicture(event) {
    let formData = new FormData()
    let file = event.target.files[0]
    let filename = event.target.files[0].name

    formData.append('file', file)
    formData.append('filename', filename)

    fetch('user-settings/profile-picture', {
        method: 'POST',
        body: formData,
        headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' },
    })   
}

And this is what I see in the payload

------WebKitFormBoundaryMDh1oCbNjpJhY4vV
Content-Disposition: form-data; name="file"; filename="my-profile-pic.png"
Content-Type: image/png

------WebKitFormBoundaryMDh1oCbNjpJhY4vV
Content-Disposition: form-data; name="filename"

my-profile-pic.png
------WebKitFormBoundaryMDh1oCbNjpJhY4vV--

But on the Laravel backend I'm getting an empty array:

public function store()
{
    dd(request()->all());

    $path = request()->file('file')->store('app/public/img/uploads');

    return $path;
}

result

[ ]

0 likes
2 replies
martinszeltins's avatar
martinszeltins
OP
Best Answer
Level 14

Ok, so the problem was that I needed to remove the content-type from headers and now it works.

await fetch(user-settings/profile-picture`, {
    method: 'POST',
    body: formData,
})
aybinv7's avatar

actually the correct way is "Content-Type": "multipart/form-data", cause you are sending in the body a data of type "FormData" object and not a "json" object

Please or to participate in this conversation.