title: How to send an HTTP request with a header parameter?
body: <p>I'm very new to javascript and web programming in general and I need some help with this. I have an HTTP request that I need to send through javascript and get need to store the output in a variable. I tried using just the call url:</p>
  
  <pre><code>https://api.fantasydata.net/nfl/v2/JSON/PlayerSeasonStats/2015
  </code></pre>
  
  <p>But it returns an authentication error because I didn't send my API key and it doesn't show me how to do it just in the URL. The API key is listed as a header and not a paramater and I'm not sure what to do with that. I tried using the XMLHttpRequest() class but I'm not quite sure I understand exactly what it does nor could I get it to work. </p>
  
  <p>The actual HTTP Request </p>
  
  <pre><code>GET https://api.fantasydata.net/nfl/v2/JSON/PlayerSeasonStats/2015 HTTP/1.1
  Host: api.fantasydata.net
  Ocp-Apim-Subscription-Key: ••••••••••••••••••••••••••••••••
  </code></pre>
  
  <p>I just need to figure out how to send that request along with the key and how to store the JSON doc it returns as a variable in javascript. </p>
  
  <p>EDIT: This is what I have so far:</p>
  
  <pre><code>function testingAPI(){
  var key = "8a1c6a354c884c658ff29a8636fd7c18";
  httpGet("https://api.fantasydata.net/nfl/v2/JSON/PlayerSeasonStats/2015",key    );
  alert(xmlHttp.responseText);
  var x = 0;
  }
  
  function httpGet(theUrl,key)
  {
  var xmlHttp = new XMLHttpRequest();
  
  xmlHttp.open( "GET", theUrl, false ); // false for synchronous request
  xmlHttp.setRequestHeader("Ocp-Apim-Subscription-Key",key);
  xmlHttp.send( null );
  return xmlHttp.responseText;
  }
  </code></pre>
  
  <p>Thank you!</p>