1

I am new to VBscript and am looking for some help to do a POST to an API and pass it a JSON string containing id, password and a scope, then get an answer and parse it. Here is the call I need to make:

POST https://integrations.ezyvet.com/call/getAccessToken { "partner_id": "id8888", "client_id": "id12345", "client_secret": "secret12345", "grant_type": "client_credentials", "scope": “read-diagnosticresult,read-diagnosticresultitem, read-diagnosticrequest,write-diagnosticresult,write-diagnosticresultitem" }

Here is my code:

Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set outFile = fso.CreateTextFile("c:\temp\JSONoutput.txt", True)
set json = CreateObject("Chilkat_9_5_0.JsonObject")
jsonStr = "{""partner_id"": ""id8888"", ""client_id"": ""id12345"", ""client_secret"": ""secret12345"", ""grant_type"": ""client_credentials"", ""scope"": ""read-diagnosticresult,read-diagnosticresultitem, read-diagnosticrequest,write-diagnosticresult,write-diagnosticresultitem""}"

success = json.Load(jsonStr)

If (success <> 1) Then
    outFile.WriteLine(json.LastErrorText)
    WScript.Quit
End If
set http = CreateObject("Chilkat_9_5_0.Http")

I need to make my POST here and get a response and am not sure how. Please help.

Thanks a million.

Roy Scheffers
  • 3,832
  • 11
  • 31
  • 36
Ahmed
  • 19
  • 1
  • 1
  • 3

1 Answers1

1

Hi and welcome to stack overflow! You have tagged chilkat in your question, however you haven addressed it anywhere in the body or the tittle of it, so I was not sure if answer pointing this or not, so I will try to make a bit of both.

without chilkat

You can do this in pure vbs by using ajax, the short answer would be

Dim request
Set request = CreateObject("MSXML2.XMLHTTP")
request.open "GET", "http://www.example.com", False '(1)
request.send infoToSend '(2)
'(3)
  1. Here you set eather "POST" or "GET"
  2. infoToSend contains the information data, formatted as "key=value&key2..."
  3. request.responseText here contains the servers answer as text, parse it as json if you need You can find information here.

with chilkat

If you still want to use chilkat the main documentation of the http object is here, here is everything you need. If you need an example tho here I've found two: making a request: https://www.example-code.com/vbscript/http_xmlHttpRequestVerbs.asp sending a json: https://www.example-code.com/vbscript/http_put_json.asp

I wont paste it here because its too long but the core part of your interest is that you is:

set request = CreateObject("Chilkat_9_5_0.HttpRequest") '(1)
request.HttpVerb = "PUT" '(2)
success = request.LoadBodyFromString(xmlStr,"utf-8") '(3)
Set response = http.SynchronousRequest(endpointDomain,endpointPort,endpointSsl,request)' (4)
  1. you have to create a httpRequest
  2. you set here eather get or post 3.you load here your content, it is your json or what you will send but formatted appropiately
  3. here you have response contain a HttpResponse object with the result

documentation on the HttpResponse, and the HttpRequest