For learning purposes, I'd like to know how to create a FormData object.
Using the FormData api, I can send a file like this :
let data = new FormData();
data.append('avatar', avatar);
var xhr = new XMLHttpRequest();
xhr.open('POST', '/api_enpoint', true);
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.setRequestHeader('X-CSRF-TOKEN', document.head.querySelector('meta[name="csrf-token"]').content);
xhr.send(data);
"avatar", appended to the FormObject, is a "File" object.
How can I perform the "FormData" work manually ?
That is, creating the full object, and send it with xhr.send(data)
Is there an example somewhere ? I can't find code that works.
I've read the RFC, but it does not help me that much.
Edit: My question is different than How to manually create multipart/form-data
My case is specifically for sending a file. And the other question does not handle that. There's no working solution at all for both questions.