Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sending multipart/form-data with PUT request doesn't work in Laravel

I am trying to send an HTTP PUT request with "Content-Type": "multipart/form-data" to a Laravel application. When I change the method to POST it works.

$a = $request->all(); // With PUT this is empty but with POST it works fine. 

The client-side executes the following code:

axios({
    method: "post", // when I try method:"PUT" and change the content type 
    url: "/api/offer",
    data: fd,
    headers: {"Content-Type": "multipart/form-data"} // here change to "x-www-form-urlencoded" it the $a array on backend is empty! 
}).then(response => {
    console.log("/offer/" + response.data)
    if (response.data)
        window.location.replace("/offer/" + this.offer.id);
    else {
        console.log("show a message that something went wrong! ")
    }
}).catch(function (error) {
})

I could not find anywhere in the docs that PUT can’t send "multipart/form-data"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

So, can PUT send "multipart/form-data" or only POST can do that in general or it is only a PHP / Laravel Issue?

Edit:
Also, what difference does it make to use PUT instead of POST other than to comply with HTTP protocol and CRUD operation properly?

>Solution :

Laravel (HTML Forms) do not work great with Put requests so you’ll need to spoof a POST request as if it was a PUT or PATCH request. On Axios you use the .post verb but within your form data you append

_method: "put"

Information from the official documentation:
https://laravel.com/docs/8.x/routing#form-method-spoofing


Excerpt from the documentation:

HTML forms do not support PUT, PATCH, or DELETE actions. So, when defining PUT, PATCH, or DELETE routes that are called from an HTML form, you will need to add a hidden _method field to the form. The value sent with the _method field will be used as the HTTP request method

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading