2

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.

The instructions are as follows:

  1. Pick a page on the NHS website, for example: https://www.nhs.uk/conditions/acne.
  2. Make a note of the path, for example: conditions/acne.
  3. Using a tool such as curl, Postman or your web browser, make a GET request to https://api.nhs.uk/content/acne with a valid subscription key subscription‑key: {subscription-key} in the request header.
  4. You’ll receive a JSON response structured using schema.org and the fields for this are explained in the following documentation....

From https://developer.api.nhs.uk/documentation/content-api

So, I wrote the following...

<%
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 "<h1>The HTML text</h1><xmp>"
Response.Write xml.responseText
Set xml = Nothing
%>

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." }

They have example scripts in 5 different languages but not ASP or even ASP.NET

Any ideas what I can try to get this working?

Thanks

EDIT

Trying the method suggested here How can I post data using cURL in asp classic? ...

<%
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) <- 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: " & http.Status & " " & http.StatusText)
End If
%>

This gives me Invalid procedure call or argument: 'SetRequestHeader'

EDIT WITH SOLUTION

Working code with hyphen issue fixed...

<%
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: " & http.Status & " " & http.StatusText)
End If
%>

Thanks Lankymart!

Shred
  • 45
  • 7
  • Does this answer your question? [How can I post data using cURL in asp classic?](https://stackoverflow.com/questions/37462580/how-can-i-post-data-using-curl-in-asp-classic) – user692942 Mar 31 '20 at 22:24
  • First, remove the `On Error Resume Next` as it will just hide important errors might shed light and why it isn't working. Also the API documentation link is incorrect. – user692942 Mar 31 '20 at 22:26
  • Hi Lankymart, I tried that but it didn't work for me. It is set up for POSTing a querystring rather than GET, which is what I need. I also get "Invalid procedure call or argument: 'SetRequestHeader'" errors. – Shred Apr 01 '20 at 08:22
  • So you change `"POST"` to `"GET"` it's just a simple verb change? – user692942 Apr 01 '20 at 08:24
  • I'll edit the original post to show where I got to with that attempt – Shred Apr 01 '20 at 08:26
  • Did you try adjusting it using the example in the duplicate? – user692942 Apr 01 '20 at 08:28
  • Hi Lanky, not sure what you mean by 'the example in the duplicate'? Sorry if I'm being thick! – Shred Apr 01 '20 at 08:33
  • Just tried my example and it returns the same error, really weird as this used to work. Something has changed since it was written. – user692942 Apr 01 '20 at 09:14
  • 1
    Worked it out the character `‑` you are using isn't `-` and it can't understand it. I replaced it and ran the code and receive `Server returned: 401 Unauthorized`. – user692942 Apr 01 '20 at 09:35
  • 1
    @Lankymart Do you mean in ``subscription-key``? Great problem solving. It can be so hard to find character errors like that. – Daniel Nordh Apr 01 '20 at 09:52
  • 1
    @DanielNordh yeah, it's not a hyphen and likely some non-breaking equivalent which causes VBScript to throw `Invalid procedure call or argument`. – user692942 Apr 01 '20 at 10:04

1 Answers1

2

Tried your take on the duplicate example and it returned

Invalid procedure call or argument: 'SetRequestHeader'

This puzzled me as that code had been tested before and work fine so what changed? So I dug into the SetRequestHeader method calls.

Turns out the error only occurs on this line;

Call .SetRequestHeader("subscription‑key", "MY-API-KEY-HERE")

In the end, removed subscription‑ from the header name and it worked without causing a compilation error.

That led me to check the hyphen in the code using Asc("‑") and comparing that with a standard hyphen and sure enough they are different.

<%
Response.Write Asc("‑") & "<br />" 'From the code
Response.Write Asc("-") & "<br />" 'Standard hyphen
%>

Output:

-15454
45

Replaced the character with a standard hyphen the error has gone and the code runs returning;

Server returned: 401 Unauthorized
user692942
  • 16,398
  • 7
  • 76
  • 175
  • 1
    You absolute hero! That works now. Thank you so much!! – Shred Apr 01 '20 at 10:19
  • 1
    Good catch , Lankymart! – Flakes Apr 01 '20 at 17:12
  • I have one more hurdle to get over now with this but I'm not sure whether to add it to this question or start a new one. It's about adding further information to the 'Body' of the request. The branch of the APIs I need has this additional requirement I wasn't aware of until now. – Shred Apr 06 '20 at 09:25
  • @Shred if you are adding information to the 'Body' of the request that's a `POST` and you pass the `Body` as an argument of the `Send()` method. – user692942 Apr 06 '20 at 09:28
  • Yes, they switched it to POST for this branch of the APIs for some reason. I'll edit the question to show what I have so far, thanks – Shred Apr 06 '20 at 09:41
  • @Shred you'll be best asking another question instead of dirtying this one. Also, you will probably find there is [already a duplicate that answers](https://stackoverflow.com/a/37462944/692942) the question already. – user692942 Apr 06 '20 at 09:46
  • Ah, sorry, just added it before seeing your comment. I did try the duplicate method but something is breaking it. – Shred Apr 06 '20 at 09:57
  • I'll take it out. New question is here - https://stackoverflow.com/questions/61057258/problem-adding-body-content-to-winhttprequest-post-request-in-classic-asp – Shred Apr 06 '20 at 10:03