0

am using Request.ServerVariables to get webhook response from GoCardless in classic asp which is calling a page on my server http:/www.example.com/webhook.asp

My code in webhook.asp:

For Each var in Request.ServerVariables 
    WriteLog var & " = " & Request.ServerVariables(var) , "gocardless"
Next 

the output is ok, i can read

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

my code works fine but am not able to retrive the json coming with

i must get the full response information as shown in Gocardless Guide

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": {}
    }
  ]
}

what code should i add to get the json response located in the header

thx

  • i donno why people degraded my question because it's not clear. well i guess it's clear enough. i need to get the json content in "Corps" tag. – SECURIDOC Middle East North A Dec 21 '19 at 18:31
  • The request is malformed, the request headers form is incorrect, it should be `name: value`. Are you sure the JSON is in the header, that doesn’t appear to be the case and I’ve never heard if that before. – user692942 Dec 23 '19 at 08:57

2 Answers2

0

well after more than 48 hours of google search this fixed my problem

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
  • That doesn’t give you the JSON, that reads the entire request as binary then converts it to a string. The JSON is in the header so this shouldn’t work, `Request.BinaryRead()` will return the body of the request not the headers. So if this works then your original request in the question is wrong / misleading and the JSON us actually in the body if the request as you would expect. – user692942 Dec 23 '19 at 08:54
  • @Lankymart you are right. i just read the json but am not able to get nodes values. my code returns empty values. i think i will save the binary read into a json file and then reload the json file with a parser unless you have other suggestions – SECURIDOC Middle East North A Dec 23 '19 at 09:34
0

ok guys thanks all for your hel but this is the complete solution that helped solving my problem and it works great

<!-- #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

%>