Asp classic 如何从标头检索json
我使用Request.ServerVariables从经典asp中的GoCardless获取webhook响应,它正在调用我的服务器http:/www.example.com/webhook.asp上的页面 我在webhook.asp中的代码:Asp classic 如何从标头检索json,asp-classic,webhooks,gocardless,Asp Classic,Webhooks,Gocardless,我使用Request.ServerVariables从经典asp中的GoCardless获取webhook响应,它正在调用我的服务器http:/www.example.com/webhook.asp上的页面 我在webhook.asp中的代码: For Each var in Request.ServerVariables WriteLog var & " = " & Request.ServerVariables(var) , "gocardless" Next
For Each var in Request.ServerVariables
WriteLog var & " = " & Request.ServerVariables(var) , "gocardless"
Next
输出正常,我可以阅读
Content-Length: 353
Content-Type: application/json
Accept: */*
Accept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3
Host: admin.controle-reglementaire.fr
User-Agent: gocardless-webhook-service/1.1
Origin: https://api.gocardless.com
Webhook-Signature: 71ef0f915569e082f090f5150fdf4144be4fed242b1253ad620544c4dd8d615a
我的代码工作正常,但无法检索附带的json
我必须获得GoCardles指南中所示的完整响应信息
Originhttps://api.gocardless.com
User-Agentgocardless-webhook-service/1.1
Content-Typeapplication/json
Webhook-Signature71ef0f915569e082f090f5150fdf4144be4fed242b1253ad620544c4dd8d615a
Corps
{
"events": [
{
"id": "EVTESTC4TEBZP2",
"created_at": "2019-12-21T10:18:30.168Z",
"resource_type": "payments",
"action": "failed",
"links": {
"payment": "index_ID_123"
},
"details": {
"origin": "bank",
"cause": "insufficient_funds",
"scheme": "sepa_core",
"reason_code": "AM04",
"description": "The customer's account had insufficient funds to make this payment."
},
"metadata": {}
}
]
}
我应该添加什么代码来获取位于标头中的json响应
thx经过48个多小时的谷歌搜索,这解决了我的问题
Dim lngBytesCount, bstring
If Request.TotalBytes > 0 Then
lngBytesCount = Request.TotalBytes
bstring= BytesToStr(Request.BinaryRead(lngBytesCount))
response.Clear
WriteLog bstring , "gocardless"
end if
Function BytesToStr(bytes)
Dim Stream
Set Stream = Server.CreateObject("Adodb.Stream")
Stream.Type = 1 'adTypeBinary
Stream.Open
Stream.Write bytes
Stream.Position = 0
Stream.Type = 2 'adTypeText
Stream.Charset = "UTF-8"
BytesToStr = Stream.ReadText
try = BytesToStr
Stream.Close
Set Stream = Nothing
End Function
好的,伙计们,谢谢大家的帮助,但这是帮助解决我问题的完整解决方案,效果很好
<!-- #include file="aspJSON1.17.asp"-->
<%
dim filename : filename = Request.ServerVariables("HTTP_Webhook-Signature")
'response.write "filename = " & filename
'---------------------------------------------------------------------------------------------------
Dim lngBytesCount, bstring
If Request.TotalBytes > 0 Then
lngBytesCount = Request.TotalBytes
response.ContentType = "application/json;charset=UTF-8"
bstring= BytesToStr(Request.BinaryRead(lngBytesCount))
'response.Clear
end if
'response.write bstring
'---------------------------------------------------------------------------------------------------
WriteLog bstring , filename
'---------------------------------------------------------------------------------------------------
Set oJSON = New aspJSON
oJSON.loadJSON bstring
For Each record In oJSON.data("events")
Set this = oJSON.data("events").item(record)
Response.Write "<p>" & this.item("id") '& " | " & this.item("charge_date") & " | " & this.item("amount") & " | " & this.item("description") & " | " & this.item("status") & " | " & this.item("links")("mandate") & " | " & this.item("links")("subscription") & "<p>"
Next
Set oJSON = Nothing
'---------------------------------------------------------------------------------------------------
Function BytesToStr(bytes)
Dim Stream
Set Stream = Server.CreateObject("Adodb.Stream")
Stream.Type = 1 'adTypeBinary
Stream.Open
Stream.Write bytes
Stream.Position = 0
Stream.Type = 2 'adTypeText
Stream.Charset = "UTF-8"
BytesToStr = Stream.ReadText
try = BytesToStr
Stream.Close
Set Stream = Nothing
End Function
'---------------------------------------------------------------------------------------------------
sub WriteLog(LogInfo, FileName)
dim FSO, Inf, dir, Fnm
Set FSO = Server.CreateObject("Scripting.FileSystemObject")
dir = "D:\webserver\experthost\trace"
Fnm = dir & "\" & FileName & ".json"
' Ouverture du fichier
' Fnm : nom du fichier
' 8 : mode append
' true : le fichier est crée s'il n'existe pas
set inF = FSO.OpenTextFile(Fnm,2,true)
'*******************************************
inF.writeLine LogInfo
inF.close
set inF = nothing
end sub
%>
0那么
LNGBytes=Request.TotalBytes
response.ContentType=“application/json;charset=UTF-8”
bstring=BytesToStr(Request.BinaryRead(lngbytesunt))
“回答,明白了
如果结束
'response.write bstring
'---------------------------------------------------------------------------------------------------
WriteLog bstring,文件名
'---------------------------------------------------------------------------------------------------
Set oJSON=New aspJSON
oJSON.loadJSON-bstring
对于oJSON.data(“事件”)中的每条记录
设置此项=oJSON.data(“事件”).项(记录)
回复。填写“”&本项目(“id”)&“&”本项目(“收费日期”)&“&”本项目(“金额”)&“&”本项目(“说明”)&“&”本项目(“状态”)&“&”本项目(“链接”)&“委托书”)&“&”本项目(“链接”)&“订阅”)和“”
下一个
设为零
'---------------------------------------------------------------------------------------------------
函数BytesToStr(字节)
暗流
Set Stream=Server.CreateObject(“Adodb.Stream”)
Stream.Type=1'adTypeBinary
小溪,打开
流。写入字节
Stream.Position=0
Stream.Type=2'adTypeText
Stream.Charset=“UTF-8”
BytesToStr=Stream.ReadText
try=bytestost
关闭
设置流=无
端函数
'---------------------------------------------------------------------------------------------------
子写入日志(LogInfo,文件名)
dim FSO、Inf、dir、Fnm
设置FSO=Server.CreateObject(“Scripting.FileSystemObject”)
dir=“D:\webserver\experthost\trace”
Fnm=dir&“\”文件名&“.json”
“费希尔酒店
Fnm:nom du fichier
'8:模式附加
真的:这是一个不存在的世界
set inF=FSO.OpenTextFile(Fnm,2,true)
'*******************************************
inF.writeLine登录信息
inF.close
set inF=无
端接头
%>
我不知道人们为什么贬低我的问题,因为它不清楚。我想这已经足够清楚了。我需要在“Corps”标记中获取json内容。请求格式不正确,请求标题表单不正确,应该是name:value
。你确定JSON在标题中吗?情况似乎不是这样,我以前也从未听说过。这不会给你JSON,它将整个请求读取为二进制,然后将其转换为字符串。JSON位于标头中,因此这不应该起作用,Request.BinaryRead()
将返回请求的主体而不是标头。因此,如果这样做有效,那么您在问题中的原始请求是错误的/误导性的,如果请求如您所期望的那样,JSON实际上会出现在正文中。@Lankymart您是对的。我只是读取json,但无法获取节点值。我的代码返回空值。我想我会将读取的二进制文件保存到json文件中,然后使用解析器重新加载json文件,除非您有其他建议