Link to home
Start Free TrialLog in
Avatar of GeneBe
GeneBeFlag for United States of America

asked on

VBScript to do a POST to a URL, get the response and parse to response so each item can be written to screen

I get a json reponse from an api call with the URL below. IP0054870896 is the IdKey

This URL http://localhost:44382/api/IPauthinfo/IpauthId/IP0054870896 returns the data below. when it is entered in IE it asked to open the json file and I get the data below. I want to write a vbscript code that will POST to http://localhost:44382/api/IPauthinfo/IpauthId/IP0054870896, get the response and iterate through the json object and write each item to the screen.

This is the result from IE
{"authId":null,"authorizationNbr":"IP0054870896","servicingProvNPI":null,"servicingProvName":null,"refAffNbr":null,"ymdEff":"6/8/2020 12:00:00 AM","ymdEnd":"6/10/2020 12:00:00 AM","unitsApproved":null,"unitsUsed":null,"aScode":"ME","aRcode":"M6","diagCode":null,"locationCode":null,"procedureCode":null}


This is my code but it does not work.



Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
URL = "http://localhost:44382/api/IPauthinfo/IpauthId/IP0054870896"
objHTTP.Open "POST", URL, False

objHTTP.setRequestHeader "Content-Type", "application/json"
objHTTP.setRequestHeader "CharSet", "charset=UTF-8"
objHTTP.setRequestHeader "Accept", "application/json"


objHTTP.send()

' Output error message to std-error and happy message to std-out. Should
' simplify error checking
If objHTTP.Status >= 400 And objHTTP.Status <= 599 Then
      Wscript.Echo "Error Occurred : " & objHTTP.status & " - " & objHTTP.statusText
Else
      Wscript.Echo "Success : " & objHTTP.status & " - " & objHTTP.ResponseText
End If
Avatar of oBdA
oBdA

Any specific reason you're trying to do that using the utterly outdated VB Script?
This is a one-liner in PowerShell, and Invoke-RestMethod will return the json already converted to a custom object.
Invoke-RestMethod -Uri http://localhost:44382/api/IPauthinfo/IpauthId/IP0054870896 -UseBasicParsing

Open in new window

Output should look like this, based on the json you posted above:
authId            :
authorizationNbr  : IP0054870896
servicingProvNPI  :
servicingProvName :
refAffNbr         :
ymdEff            : 6/8/2020 12:00:00 AM
ymdEnd            : 6/10/2020 12:00:00 AM
unitsApproved     :
unitsUsed         :
aScode            : ME
aRcode            : M6
diagCode          :
locationCode      :
procedureCode     :

Open in new window

You can obviously save the results in a variable and access its properties:
$response = Invoke-RestMethod -Uri http://localhost:44382/api/IPauthinfo/IpauthId/IP0054870896 -UseBasicParsing
$response.authorizationNbr

Open in new window

Avatar of GeneBe

ASKER

I need to use VBScript cause that is the interface language between to systems. will powershell command work same as VBScript?
Anything you can do with VB Script, you can do in PowerShell.
So what exactly is it you're trying to achieve here?
Avatar of GeneBe

ASKER

there is a third party forms program that needs data populated and in order to do that they have a vbscript program the is used and a data interface/API.
I wrote a web service to access the data. the I use the API call for the web service in the VBScript program to pass the data to the third party program. I use http POST in the VBScript. so I need the code or commans to do the api call and parse the json response.text and split the data.


So the forms program starts a VB Script, which is supposed to return the data in a given format? And you can edit said script?
And that format would be?
Avatar of GeneBe

ASKER

yes I added this but it does not work
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
                          URL = "http://localhost:44382/api/IPauthinfo/IpauthId/IP0054870896"
                          objHTTP.Open "POST", URL, False
                          
                          objHTTP.setRequestHeader "Content-Type", "application/json"
                          objHTTP.setRequestHeader "CharSet", "charset=UTF-8"
                          objHTTP.setRequestHeader "Accept", "application/json"
                          
                          
                          objHTTP.send()
                          
                          ' Output error message to std-error and happy message to std-out. Should
                          ' simplify error checking
                          If objHTTP.Status >= 400 And objHTTP.Status <= 599 Then
                                Wscript.Echo "Error Occurred : " & objHTTP.status & " - " & objHTTP.statusText
                          Else
                                Wscript.Echo "Success : " & objHTTP.status & " - " & objHTTP.ResponseText
                          End If
















"it does not work" is not an error description that will help anybody help you.
Have you tried if the PowerShell command above works?
it does not work

Do you get an error message?
What is the http status?

In some past VB-centric code I've written, I had to check on some other completion status (ReadyState = 4) before I could accurately check the http status.  Otherwise, the code couldn't be sure that the call had completed to the point where the status could be checked.
Avatar of GeneBe

ASKER

Error message:
User generated image
This is the code:
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
URL = "https://localhost:44382/api/IPauthinfo/IP0054870896"
objHTTP.Open "POST", URL, False
objHTTP.setRequestHeader "Content-Type", "application/xml"
objHTTP.setRequestHeader "CharSet", "charset=UTF-8"
objHTTP.setRequestHeader "Accept", "application/xml"
objHTTP.send()
' Output error message to std-error and happy message to std-out. Should
' simplify error checking
If objHTTP.Status >= 400 And objHTTP.Status <= 599 Then
 Wscript.Echo "Error Occurred : " & objHTTP.status & " - " & objHTTP.statusText
Else
 Wscript.Echo "Success : " & objHTTP.status & " - " & objHTTP.ResponseText
End If

ASKER CERTIFIED SOLUTION
Avatar of aikimark
aikimark
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of GeneBe

ASKER

how do I validate the readystate in the vbscript code?

Avatar of GeneBe

ASKER

I changed from
objHTTP.Open "POST", URL, False
to 
objHTTP.Open "GET", URL, False
and this worked.
Thank you everyone!

Avatar of GeneBe

ASKER

thank you