在经典ASP中将JSON内容添加到WinHttpRequest POST请求时出现问题 [英] Problem adding JSON content to WinHttpRequest POST request in classic ASP

查看:663
本文介绍了在经典ASP中将JSON内容添加到WinHttpRequest POST请求时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用NHS API检索数据,说明如下...

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
}

在此处遵循答案如何我可以在asp classic中使用cURL发布数据吗?我试过了...

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

...但是这给了我服务器退回:400错误的请求".几乎可以肯定,我不知道如何正确执行此操作.我要去哪里错了?谢谢

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

推荐答案

问题在于API需要一个application/json主体,因此您需要传递该主体而不是application/x-www-form-urlencoded数据.

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

您可以将 JSON构造为字符串,但是它需要符合JSON结构,否则您可能会得到再次是HTTP 400 Bad Request.

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.

data替换为;

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

您还可以使用一些现有库来为您构建和解析JSON.

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

我个人建议- RCDMK的JSON对象类

这篇关于在经典ASP中将JSON内容添加到WinHttpRequest POST请求时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆