title: Passing a subscription key as a request header with msxml2.ServerXMLHTTP - Classic ASP/VB
body: <p>I'm trying to pull data from an NHS API using a little bit of classic ASP (all I know I'm afraid) but am struggling to successfully pass the subscription key to the API.</p>
  
  <p>The instructions are as follows:</p>
  
  <ol>
  <li>Pick a page on the NHS website, for example: <a href="https://www.nhs.uk/conditions/acne" rel="nofollow noreferrer">https://www.nhs.uk/conditions/acne</a>.</li>
  <li>Make a note of the path, for example: conditions/acne.</li>
  <li>Using a tool such as curl, Postman or your web browser, make a GET request to <a href="https://api.nhs.uk/content/acne" rel="nofollow noreferrer">https://api.nhs.uk/content/acne</a> with a valid subscription key subscription‑key: {subscription-key} in the request header.</li>
  <li>You’ll receive a JSON response structured using schema.org and the fields for this are explained in the following documentation....</li>
  </ol>
  
  <p>From <a href="https://developer.api.nhs.uk/documentation/content-api" rel="nofollow noreferrer">https://developer.api.nhs.uk/documentation/content-api</a></p>
  
  <p>So, I wrote the following...</p>
  
  <pre><code>&lt;%
  Set xml = Server.CreateObject("MSXML2.ServerXMLHTTP")
  xml.Open "GET", "https://api.nhs.uk/conditions/abdominal-aortic-aneurysm-screening/", False
  on error resume next
  xml.setRequestHeader "subscription‑key", "MY-API-KEY-HERE"
  xml.setRequestHeader "Content-Type", "application/json"
  xml.setRequestHeader "Accept", "application/json"
  xml.Send
  Response.Write "&lt;h1&gt;The HTML text&lt;/h1&gt;&lt;xmp&gt;"
  Response.Write xml.responseText
  Set xml = Nothing
  %&gt;
  </code></pre>
  
  <p>This just gives me the following response:
  { "statusCode": 401, "message": "Access denied due to missing subscription key. Make sure to include subscription key when making requests to an API." }</p>
  
  <p>They have example scripts in 5 different languages but not ASP or even ASP.NET</p>
  
  <p>Any ideas what I can try to get this working?</p>
  
  <p>Thanks</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Trying the method suggested here <a href="https://stackoverflow.com/questions/37462580/how-can-i-post-data-using-curl-in-asp-classic">How can I post data using cURL in asp classic?</a> ...</p>
  
  <pre><code>&lt;%
  Dim http: Set http = Server.CreateObject("WinHttp.WinHttpRequest.5.1")
  Dim url: url = "https://api.nhs.uk/conditions/abdominal-aortic-aneurysm-screening/"
  'Dim data: data = "something=this" - took this out as its a querystring for POST
  
  With http
    Call .Open("GET", url, False)
    'Call .SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
    Call .SetRequestHeader("subscription‑key", "MY-API-KEY-HERE")
    'Call .Send(data) &lt;- the data was the querystring, so not relevant here
    Call .Send()
  End With
  
  If Left(http.Status, 1) = 2 Then
    'Request succeeded with a HTTP 2xx response, do something...
  Else
    'Output error
    Call Response.Write("Server returned: " &amp; http.Status &amp; " " &amp; http.StatusText)
  End If
  %&gt;
  </code></pre>
  
  <p>This gives me Invalid procedure call or argument: 'SetRequestHeader'</p>
  
  <p><strong>EDIT WITH SOLUTION</strong></p>
  
  <p>Working code with hyphen issue fixed...</p>
  
  <pre><code>&lt;%
  Dim http: Set http = Server.CreateObject("WinHttp.WinHttpRequest.5.1")
  Dim url: url = "https://api.nhs.uk/conditions/abdominal-aortic-aneurysm-screening/"
  
  With http
    Call .Open("GET", url, False)
    Call .SetRequestHeader("subscription-key", "MYKEYHERE")
    Call .Send()
  End With
  
  If Left(http.Status, 1) = 2 Then
    'Request succeeded with a HTTP 2xx response, do something...
    Response.Write http.responseText
  Else
    'Output error
    Call Response.Write("Server returned: " &amp; http.Status &amp; " " &amp; http.StatusText)
  End If
  %&gt;
  </code></pre>
  
  <p>Thanks Lankymart!</p>