title: Cannot add CSRF token to XMLhttpRequest for Django POST request
body: <p>CSRF token is not being added to the header of the XMLhttpRequest. </p>
  
  <p>To make sure all the other code works, I've tried using a GET request to the server and everything works as it should. However when I add the CSRF token to the header the code stops running when adding the header. </p>
  
  <pre><code>// [PROBLEM CODE]
  // Initialize new request
  const request = new XMLHttpRequest();
  const currency = document.querySelector('#currency').value;
  
  request.setRequestHeader({'csrfmiddlewaretoken': csrftoken}); // [&lt;---HERE]
  request.setRequestHeader("Content-Type", "text/plain;charset=UTF-8"); // [&lt;---HERE]
  
  request.open('POST', '/convert');
  
  
  // [THIS WORKS]
  // Initialize new request
  const request = new XMLHttpRequest();
  const currency = document.querySelector('#currency').value;
  request.open('GET', '/convert');
  request.send();
  </code></pre>
  
  <p>When I take out the two problem lines of code, setRequestHeader and make a POST request. The error message I get is "Forbidden (CSRF token missing or incorrect.): /convert"</p>