【问题标题】:Passing a subscription key as a request header with msxml2.ServerXMLHTTP - Classic ASP/VB使用 msxml2.ServerXMLHTTP 将订阅密钥作为请求标头传递 - 经典 ASP/VB
【发布时间】:2020-04-06 18:04:28
【问题描述】:

我正在尝试使用一点经典 ASP 从 NHS API 中提取数据(我只知道我很害怕),但我很难成功地将订阅密钥传递给 API。

说明如下:

  1. 在 NHS 网站上选择一个页面,例如:https://www.nhs.uk/conditions/acne
  2. 记下路径,例如:conditions/acne。
  3. 使用 curl、Postman 或 Web 浏览器等工具,向 https://api.nhs.uk/content/acne 发出 GET 请求,并在请求标头中使用有效的订阅密钥订阅密钥:{subscription-key}。
  4. 您将收到一个使用 schema.org 构建的 JSON 响应,其字段在以下文档中进行了说明....

来自https://developer.api.nhs.uk/documentation/content-api

所以,我写了以下...

<%
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
%>

这只是给了我以下回应: { "statusCode": 401, "message": "由于缺少订阅密钥,访问被拒绝。请确保在向 API 发出请求时包含订阅密钥。" }

他们有 5 种不同语言的示例脚本,但没有 ASP 甚至 ASP.NET

有什么想法可以让这个工作吗?

谢谢

编辑

尝试这里建议的方法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
%>

这给了我无效的过程调用或参数:'SetRequestHeader'

使用解决方案编辑

已修复连字符问题的工作代码...

<%
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
%>

感谢 Lankymart!

【问题讨论】:

  • 这能回答你的问题吗? How can I post data using cURL in asp classic?
  • 首先,删除On Error Resume Next,因为它只会隐藏重要的错误,可能会揭示它为什么不起作用。 API 文档链接也不正确。
  • 算出你使用的字符不是-,它无法理解。我替换它并运行代码并收到Server returned: 401 Unauthorized
  • @Lankymart 你的意思是subscription-key?很好的解决问题。很难找到这样的字符错误。
  • @DanielNordh 是的,它不是连字符,可能是一些非破坏性的等价物导致 VBScript 抛出 Invalid procedure call or argument

标签: vbscript asp-classic xmlhttprequest


【解决方案1】:

尝试了你对重复示例的看法,它返回了

无效的过程调用或参数:'SetRequestHeader'

这让我感到困惑,因为该代码之前已经过测试并且工作正常,那么发生了什么变化? 所以我深入研究了SetRequestHeader 方法调用。

原来错误只发生在这一行;

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

最后,从标头名称中删除了subscription‑,它可以正常工作而不会导致编译错误。

这导致我使用 Asc("‑") 检查代码中的连字符并将其与标准连字符进行比较,果然它们是不同的。

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

输出:

-15454
45

用标准连字符替换字符,错误消失,代码运行返回;

Server returned: 401 Unauthorized

【讨论】:

  • 你绝对的英雄!现在可以了。非常感谢!!
  • 好消息,Lankymart!
  • 我现在还有一个障碍要克服,但我不确定是将它添加到这个问题中还是开始一个新的问题。这是关于向请求的“正文”添加更多信息。我需要的 API 分支有这个我之前不知道的额外要求。
  • @Shred 如果您要向POST 请求的“正文”添加信息,并且您将Body 作为Send() 方法的参数传递。
  • 是的,出于某种原因,他们为 API 的这个分支将其切换为 POST。我将编辑问题以显示我到目前为止的内容,谢谢
猜你喜欢
  • 1970-01-01
  • 2014-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-25
  • 2011-12-22
  • 1970-01-01
  • 2017-11-20
相关资源
最近更新 更多