GeneBe
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,"authorizat ionNbr":"I P005487089 6","servic ingProvNPI ":null,"se rvicingPro vName":nul l,"refAffN br":null," ymdEff":"6 /8/2020 12:00:00 AM","ymdEnd":"6/10/2020 12:00:00 AM","unitsApproved":null," unitsUsed" :null,"aSc ode":"ME", "aRcode":" M6","diagC ode":null, "locationC ode":null, "procedure Code":null }
This is my code but it does not work.
Set objHTTP = CreateObject("MSXML2.Serve rXMLHTTP")
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
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,"authorizat
This is my code but it does not work.
Set objHTTP = CreateObject("MSXML2.Serve
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
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?
So what exactly is it you're trying to achieve here?
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.
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?
And that format would be?
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
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?
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.
ASKER
Error message:
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
how do I validate the readystate in the vbscript code?
ASKER
I changed from
objHTTP.Open "POST", URL, False
to
objHTTP.Open "GET", URL, False
and this worked.
Thank you everyone!
objHTTP.Open "POST", URL, False
to
objHTTP.Open "GET", URL, False
and this worked.
Thank you everyone!
ASKER
thank you
This is a one-liner in PowerShell, and Invoke-RestMethod will return the json already converted to a custom object.
Open in new window
Output should look like this, based on the json you posted above:Open in new window
You can obviously save the results in a variable and access its properties:Open in new window