2

I am trying to retrieve data using an NHS API and the instructions are as follows...

Endpoint        https://api.nhs.uk/service-search/search?api-version=1
Method      POST
Headers     Content-Type: application/json
subscription-key: MYKEYHERE
Body        {
    "filter": "OrganisationTypeID eq 'PHA'",
    "orderby": "OrganisationName",
    "top": 25,
    "skip": 0,
    "count": true
}

Following the answer here How can I post data using cURL in asp classic? I tried this...

<%
Dim http: Set http = Server.CreateObject("WinHttp.WinHttpRequest.5.1")
StrFilter = "OrganisationTypeID eq 'PHA'"
StrFilter = Server.UrlEncode(StrFilter)
Dim url: url = "https://api.nhs.uk/service-search/search?api-version=1"
Dim data: data = "filter=" & StrFilter & "&orderby=OrganisationName&top=25&skip=0&count=true"
With http
  Call .Open("POST", url, False)
  Call .SetRequestHeader("subscription-key", "MYKEYHERE")
  Call .SetRequestHeader("Content-Type", "application/json")
  Call .Send(data)
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
%>

...but this gives me "Server returned: 400 Bad Request". It's almost certainly a case of me not knowing how to do this properly. Where am I going wrong? Thanks

user692942
  • 16,398
  • 7
  • 76
  • 175
Shred
  • 45
  • 7

2 Answers2

1

The issue is the API expects an application/json body, so you need to pass that instead of application/x-www-form-urlencoded data.

You can build the JSON up as a string but it needs to conform to a JSON structure or you will likely get an HTTP 400 Bad Request again.

Replace data with;

Dim data: data = "{ ""filter"": ""OrganisationTypeID eq 'PHA'"", ""orderby"": ""OrganisationName"", ""top"": 25, ""skip"": 0, ""count"": true }"

You could also use some existing libraries to build and parse JSON for you.

Personally I'd recommend - JSON object class by RCDMK

user692942
  • 16,398
  • 7
  • 76
  • 175
  • ok. I have no idea how to do that, sorry. Is Call .SetRequestBody("parameter", "value") a thing? I tried passing the body values like that instead but just get a 500 error – Shred Apr 06 '20 at 11:18
  • @Shred You just need to pass `Send()` the correctly formatted string, added an example of what `data` should look like. – user692942 Apr 06 '20 at 11:19
  • Yup, that worked. Those curly brackets are important! Thanks again Lankymart, that should be all I need to get this going. – Shred Apr 06 '20 at 11:24
  • @Shred yep, the curly brackets are important when it comes to [JSON](https://www.json.org/json-en.html) data. – user692942 Apr 06 '20 at 11:31
1

I solved this problem

url="https://test/xyzapi/api/Account/login"
Dim oXMLHttp
dim auth_token
Set oXMLHttp=Server.Createobject("MSXML2.ServerXMLHTTP.6.0")
oXMLHttp.setOption 2, 13056
oXMLHttp.open "POST", url,false
oXMLHttp.setRequestHeader "Content-Type","application/json"
oXMLHttp.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; 
Windows NT 5.0)"
strJSONToSend ="{""Username"":""test"",""Password"":""123456""}"
Dijkgraaf
  • 11,049
  • 17
  • 42
  • 54
BurhanY
  • 81
  • 6
  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 21 '21 at 15:06