Search code examples
htmlxmlhttprequest

xhr send - send file and CSRF token


I'm trying to use XMLHttpRequest to send a file object and a string (CSRF Token) to the server but I cant seem to figure out how to add the token:

xhr.open("POST", form.action, true);
xhr.setRequestHeader("X_FILENAME", file.name);
xhr.send(file);

Is sending the file, anyone lend a hand on how to attach the token?


Solution

  • Why don't you just add

    xhr.setRequestHeader('X-CSRF-TOKEN', '<YOURTOKENHERE>');
    

    before xhr.send() ?

    As far as I understand your problem, this should solve it. (Might be I misunderstood, though)