Axios/Ajax HTTP patch requests with file not working

Posted 1 year ago by rickbolton

I'm using Axios HTTP requests however, I've also tried using jQuery ajax and getting the same result


let files = e.target.files || e.dataTransfer.files;
                if (!files.length) {
                    return;
                }

                let form = new FormData();
                form.append('profile_picture', files[0]);

                axios.patch('/profile', form)
                .then((response) => {
                    console.log(response);
                });

                $.ajaxSetup({
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    }
                });

                $.ajax({
                    url: '/profile',
                    type: 'patch',
                    data: form,
                    cache: false,
                    dataType: 'json',
                    processData: false,
                    contentType: false,
                    success: function(response) {
                        console.log(response);
                    }
               });
                    

When it's a PATCH request the file does not appear in the request in Laravel. However, when I change it to POST the file appears correctly. Has anyone come across this and have a way to get this working using PATCH request? Or is it fine to use POST even though I'm updating a record?

Thanks!

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.