我有一个卷曲的例子:
{
"http_status": 200,
"status": "success",
"tranquility":
{
"tranquility":
[
{
"meaning": "the quality or state of being tranquil; calm."
}
],
"total_results": 1,
"page": 1,
"page_size": 1,
"total_pages": 1
}
}
我需要使用asp classic将此请求发送到phoneburner
然后我需要发送:
$ curl https://www.phoneburner.com/rest/1/members/ \ -H“授权:承载EibGl9i8WNi0iI3a0He6PYg1Kntpzq8y3foSxQyg”
{
"http_status": 200,
"status": "success",
"members":
{
"total_results": 1,
"members":
[
{
"user_id": "1234567",
"username": "saulgoodman",
"first_name": "Saul",
"last_name": "Goodman",
"email_address": "callsaul@bettercallsaul.com",
"date_added": "2013-01-17 17:12:13",
"phone": "9492181234",
"display_name": "Standard Account",
"billing_item_id": "131",
"subscription_status": "1",
"_link":
{
"self":
{
"href": "\/rest\/1\/members\/1234567"
}
}
}
],
"page": 1,
"page_size": 1,
"total_pages": 1
}
我如何将它们与asp classic一起发送?
答案 0 :(得分:2)
您要达到的目标是通过HTTP脚本发送带有自定义发布数据和自定义标头的HTTP POST。
ASP Classic具有两种不同的实现:VBScript和JScript。我要提供的答案是VBScript。
<%
sub DoPOST(strJsonData, strEndpointURL, strAuthorizationToken)
dim objHttp
set objHttp = server.createobject("MSXML2.ServerXMLHTTP")
with objHttp
.open "POST", strEndpointURL, false
.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
if len(strAuthorizationToken) > 0 then
.setRequestHeader "Authorization", "Bearer "& strAuthorizationToken
end if
.send strJsonData
end with
Set xmlhttp = nothing
end sub
strPostData = "{""http_status"": 200, ""status"": ""success"", ""tranquility"": { ""tranquility"": [{""meaning"": ""the quality or state of being tranquil; calm.""}],""total_results"": 1,""page"": 1,""page_size"": 1,""total_pages"": 1}}"
call DoPOST(strPostData, "https://www.phoneburner.com/rest/1/tranquility", empty)
strPostData = "{ ""http_status"": 200, ""status"": ""success"", ""members"": { ""total_results"": 1, ""members"": [{""user_id"": ""1234567"",""username"": ""saulgoodman"",""first_name"": ""Saul"",""last_name"": ""Goodman"",""email_address"": ""callsaul@bettercallsaul.com"",""date_added"": ""2013-01-17 17:12:13"",""phone"": ""9492181234"",""display_name"": ""Standard Account"",""billing_item_id"": ""131"",""subscription_status"": ""1"",""_link"":{""self"":{""href"": ""\/rest\/1\/members\/1234567""}}}],""page"": 1,""page_size"": 1,""total_pages"": 1}"
call DoPOST(strPostData, "https://www.phoneburner.com/rest/1/members/", "EibGl9i8WNi0iI3a0He6PYg1Kntpzq8y3foSxQyg")
%>
说明:
我建立了一个子例程,该子例程通过MSXML2.ServerXMLHTTP对象执行HTTP POST操作。您可以调用此子例程两次,每次只需为其提供不同的参数即可。
关于我提供的代码的一些重要点:
1)您必须对JSON字符串中的每个引号进行转义,因为ASP VBScript仅使用引号字符串来设置字符串文字,因此,通过将JSON中的引号与另一个引号加倍,可以有效地对其进行转义(例如,“ JSON”变为““ JSON”“)。
2)第二个参数是您要将数据发送到的URL。
3)第三个参数是您要在第二个POST中的HTTP标头中传递的授权令牌(即授权:Bearer EibGl9i8WNi0iI3a0He6PYg1Kntpzq8y3foSxQyg)。如果在参数列表中提供它,则该子例程将在HTTP POST中将其与Authorization标头一起发送;如果您将其保留为空白(即为空),则标题将被忽略。这是对子程序的第一次调用和第二次调用之间的重要区别。