jsonserializationvbscriptasp-classicserverxmlhttp

Set value of apiDefaults to a variable


I have been trying to find a way to extract rows of data from an SQL database, format the returned rows into a JSON format and post/send that to a JSON web API. I am using an asp JSON serializer for VBScript (.Flush) to transform the SQL data into JSON. It works great, I have the structure i need and I also have the method to send the JSON formatted data. So what's the issue you might ask.....

The issue is I can't send apiDefaults.flush to the webserver, it does not work. If I send the same data as a hardcoded variable it works perfectly and the API takes the data.

Let me explain some more.

Here is the JSON output I see on screen using apiDefaults.flush This is perfectly formatted exactly what i need.

{"authentication": { "apiKey": "123456789101112131415" }, "createProperties": [ { "name": "Property 1", "propKey": "6sf3zqq151n2wkah" }, { "name": "Property 2", "propKey": "yuf38zc4cls9hz19" } ] } 

However, I can't get this value stored into a variable. If I could simply do the following, my issue would be resolved but you can't bind a the .flush method to a variable?!?! I don't understand why as it is just a string of text in theory.

dim myJson = apiDefaults.flush

This bit of code here is where I set the values to send to the web server, my variable that holds the JSON string

'IF I USE THIS IT WORKS PERFECTLY, IT TAKES MY MANUALLY CREATED VARIABLE/STRING AND SENDS IT
oXMLHttp.send strmultiprop 

'THIS IS WHAT I FIGURED I SHOULD BE ABLE TO USE BUT IT IS NOT TREATED LIKE A VARIABLE

oXMLHttp.send apiDefaults.flush

' I TRIED THIS AND THIS WON'T WORK EITHER

oXMLHttp.send myJson

Below is my actual scripts

<% @LANGUAGE="VBSCRIPT" CODEPAGE="65001" %>
<!--#include file="JSON_2.0.4.asp"-->
<%

Function QueryToJSON(dbcomm, params)
        Dim rs, jsa, aaa, bbb
        Set rs = dbcomm.Execute(,params,1)
        Set jsa = jsArray()
        Do While Not (rs.EOF Or rs.BOF)
                Set jsa(Null) = jsObject()
                For Each col In rs.Fields
                        jsa(Null)(col.Name) = col.Value             
                Next
        rs.MoveNext
        Loop
        Set QueryToJSON = jsa

        rs.Close
End Function

  strConn = "PROVIDER=SQLOLEDB;DATA SOURCE=LOCALHOST;UID=sa;PWD=********;DATABASE=DB_Site"
  Set conn = Server.CreateObject("ADODB.Connection")
  conn.Open strConn
  query = "SELECT  wce_uid as name, uniqueid as propKey FROM wces_users WHERE Prop_Moved  = ?"
  CustomerID = "Y" 'Request.QueryString("CustomerID")
  arParams = array(CustomerID)
  Set cmd = Server.CreateObject("ADODB.Command")
  cmd.CommandText = query
  Set cmd.ActiveConnection = conn


' https://stackoverflow.com/questions/40781130/setting-up-rest-api-in-classic-asp
key = "1234567891011121314151678910"
url = "https://www.beds24.com/api/json/createProperties"


'1 PROPERTY WORKING
str1prop = "{""authentication"": { ""apiKey"": ""123456789101112131415"" }, ""createProperties"": [ { ""name"": ""New Test"", ""propKey"": ""NewTest1"" } ] }"

'2 PROPERTY WORKING
strmultiprop = "{""authentication"": { ""apiKey"": ""123456789101112131415"" }, ""createProperties"": [ { ""name"": ""3333"", ""propKey"": ""3333"" },  { ""name"": ""4444"", ""propKey"": ""4444"" } ] }"

set apiDefaults = QueryToJSON(cmd, arParams)

'3 THE FLUSH METHOD --- THIS DOES NOT SET THE VRIABLE TO THE OUTPUT I SEE ON SCREEN
dim myJson = apiDefaults.flush

apiDefaults.flush



conn.Close : Set Conn = Nothing

Dim oXMLHttp
Set oXMLHttp=Server.Createobject("MSXML2.ServerXMLHTTP.6.0")
oXMLHttp.open "POST", url,false
oXMLHttp.setRequestHeader "Content-Type", "application/json"
oXMLHttp.send apiDefaults.flush 'THIS IS THE ISSUE LINE USING THIS. if I CHANGE TO strmultiprop NO ISSUES AT ALL. 
response.write oXMLHttp.responseText
Set oXMLHttp = Nothing

%>

Any advice on this would be great.

Thanks for looking.


Solution

  • My question was answered by @SearchAndResQ

    "Try oXMLHttp.send apiDefaults.jsString, you need the value in that property"