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

asked on

API CALL IN VBSCRIPT

this api call "http://localhost:44382/api/IPauthinfo/IP0054870896
I have a c# program that give me the result in swagger:
 

Response body


Download
<AuthInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <AuthId>IP0054870940 </AuthId>
  <AuthNbr>IP0054870940</AuthNbr>
  <RequestingProvName>Sullivan, Jon M</RequestingProvName>
  <EffectiveDate>6/4/2020 12:00:00 AM</EffectiveDate>
  <ExpireDate>6/9/2020 12:00:00 AM</ExpireDate>
  <UnitsApproved>5</UnitsApproved>
  <UnitsUsed>
  </UnitsUsed>
  <AScode>ME</AScode>
  <ARcode>M6</ARcode>
  <DiagCode>J45.998</DiagCode>
  <LocationCode>31</LocationCode>
  <ProcedureCode>
  </ProcedureCode>
</AuthInfo>
 

can I have a code in VBSscript that can do the api call and write it to the screen.
Avatar of Scott Fell
Scott Fell
Flag of United States of America image

You are going to do an xmlhttp post.  I have an example here https://www.experts-exchange.com/questions/28374549/Programatically-POST-data-securly.html#a39887728  and Microsoft has a generic example as well https://support.microsoft.com/en-us/help/290591/how-to-submit-form-data-by-using-xmlhttp-or-serverxmlhttp-object 

In my example, I am sending xml to the screen
<%
if request.form("credit_card_no")<>"" then
MerchNO="1234"
MyPin="abc"
credit_card_no="request.form("credit_card_no")
'  keep adding fields
theURL = "https://demo.myvirtualmerchant.com/VirtualMerchantDemo/process.do"


'now use xmlhttp post http://support.microsoft.com/kb/290591 to send data.  Any items you want hidden form people are in your variables and posted this way.


   DataToSend = "ssl_merchant_id =MerchNO& ssl_pin=MyPin&CC_NO=credit_card_no"
   dim xmlhttp
   set xmlhttp = server.Createobject("MSXML2.ServerXMLHTTP")
   xmlhttp.Open "POST",theURL,false
   xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
   xmlhttp.send DataToSend
   Response.ContentType = "text/xml"
   Response.Write xmlhttp.responsexml.xml ' this is your response from the post but probably not used in your case
        Set xmlhttp = nothing








end if


%>

Open in new window


Instead of xmlhttp.responsexml.xml  you can use responseText if you are going to parse something else like json or raw text or html.

The trick is to send the xml, you have to build it line by line.

xml = ""
xml=xml & "<AuthInfo xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>"
xml=xml & "  <AuthId>IP0054870940 </AuthId>"
:
:
xml=xml&"</AuthInfo> "

Open in new window


Where I have
 DataToSend = "ssl_merchant_id =MerchNO& ssl_pin=MyPin&CC_NO=credit_card_no"

Open in new window

You would use
DataToSend = xml

Open in new window

Avatar of GeneBe

ASKER

I just want to Post this to theURL = "http://localhost:44382/api/IPauthinfo/IP0054870940"
and get a response.

I just explained how.  Did you give it a try with your own data?  Below may help you get started.  

theURL ="http://localhost:44382/api/IPauthinfo/IP0054870940" 
DataToSend = ""
   dim xmlhttp
   set xmlhttp = server.Createobject("MSXML2.ServerXMLHTTP")
   xmlhttp.Open "POST",theURL,false
   xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
   xmlhttp.send DataToSend
   Response.ContentType = "text/xml"
   Response.Write xmlhttp.responsexml.xml ' this is your response from the post but probably not used in your case
        Set xmlhttp = nothing

Open in new window


or

theURL ="http://localhost:44382/api/IPauthinfo/IP0054870940" 
DataToSend = ""
   dim xmlhttp
   set xmlhttp = server.Createobject("MSXML2.ServerXMLHTTP")
   xmlhttp.Open "POST",theURL,false
   xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
   xmlhttp.send DataToSend
   Response.ContentType = "text/xml"
   Response.Write xmlhttp.responsetext ' this is your response from the post but probably not used in your case
        Set xmlhttp = nothing

Open in new window



Avatar of GeneBe

ASKER

I get this error with that code.

What is the error?

Before starting you need to know:

  • GET or POST?
  • Do you need to pass any type of authorization or token?
  • What parameters if any to pass in the header?
  • What parameters to pass if any in the body or url?
  • What type of output are you expecting? 

The code I have provided is using classic asp and the vbscript Response.Write may need to be changed to Wscript.Echo.  Watch for those types of errors.
Avatar of GeneBe

ASKER

http://localhost:44382/api/IPauthinfo/IP0054870940 web service is running. when I put it in IE it returns the response. so I just want to make this call and be able to see the results in a msgbox or wscript.echo
Gene,

I am not sure where the break down is.  

I have provided all of this information already.  
The API call to the url will output text. xml or json to the body via Response.Write xmlhttp.responsetext. If you call the script using the command line, you will get out put there. I would suggest using postman. It makes working with api development easier.
Avatar of GeneBe

ASKER

it's not working for plain vbscript. I am not using asp. I think there is a conflict with VBScript and the web service I created using .NET Core 3.1. I think I will just use the json Content-Type. do  you have any plain VBScript code the uses json and prints the response to screen using Wscript echo?


ASKER CERTIFIED SOLUTION
Avatar of Scott Fell
Scott Fell
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

Thank you.

Your welcome.

As you run into an error that you are not sure of, just post a new question and make sure to include the code you are posting (obfuscating anything private) and include the exact error you are getting with the line number and word for word error detail.