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

Ligonsker's avatar

fetch + PUT gives error 419

Hi,

I've been using the fetch API for a long time with POST like so:

    let _token = document.querySelector('meta[name="csrf-token"]').content;
    let form = document.getElementById("my-form");
    let formData = new FormData(form);
    formData.append("_token", _token);
    fetch("/upload", {
        method: "POST",
        body: formData,
    })
        .then((response) => {
        })

And in my web.php the route is:

Route::post('/upload', [MyController::class, 'upload']);

Now, I wanted to test it with PUT instead, so I changed the route method in web.php:

Route::put('/upload', [MyController::class, 'upload']);

And changed the fetch method:

    fetch("/upload", {
        method: "PUT",
        body: formData,
    })
        .then((response) => {
        })

But now I get 419 Page Expired as if I don't have a token. Why?

Thanks

0 likes
2 replies
Sergiu17's avatar
Sergiu17
Best Answer
Level 60
formData.append('_token', _token);
formData.append("_method", 'PUT');
fetch('/upload', {
    method: 'POST',
    body: formData,
})

This should do send a POST request with _method property set as 'PUT'

1 like
Ligonsker's avatar

@Sergiu17 thank you, that worked - any idea why it's so cumbersome? I think I'll just stick to POST.

Update: Do you maybe have a link to the source code of that part? Just curious to see how it works

Please or to participate in this conversation.