1

I have the following web api method in my controller

    public HttpResponseMessage PostUpdateCardStatus(CardholderRequest cardholderRequest)
    {
        var cardId = cardholderRequest.CardId;

        switch (cardholderRequest.Action)
        {
            case "Enable":
                break;
            case "Disable":
                break;                
        }

        var cardholderResponse = new CardholderResponse(cardholderRequest.RequestId)
        {
            Status = "OK"
        };
        var response = Request.CreateResponse<CardholderResponse>(HttpStatusCode.OK, cardholderResponse);
        return response;
    }

This is how I'm calling it from a .NET console app

        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri("http://localhost:55208/");

            var request = new CardholderRequest()
            {
                RequestId = Guid.NewGuid().ToString(),
                CardId = "123456",
                Action = "Enable",
                LoginId = "tester",
                Password = "tester",
            };
            var response = client.PostAsJsonAsync("api/cardholders", request).Result;
            if (response.IsSuccessStatusCode)
            {
                var cardholderResponse = response.Content.ReadAsAsync<CardholderResponse>().Result;

            }

How is it possible to make the same call using VBScript?

I tried googling but I haven't come across any solid examples of calling web api methods from VB script.

Does my web api method support calls from VBScript? Or would I need some tweaking?

Null Reference
  • 11,260
  • 40
  • 107
  • 184

1 Answers1

4

I know this is a bit old now, but I solved it. Figured I would put up an answer in case anyone else gets stuck with this task. I needed it because at a new job I have to work with a product called SmarTeam, which does a lot in vbscript, but we needed more power.

I made a standard ASP.Net Web API 2. Follow any tutorial out there to make one. Mine is pretty close to this.

http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api

Next I made a vbscript file, first I need some variables. I got most of this from several different sites, and put it together, so it is stolen from a few different sources

Dim oXMLDoc ' this will hold the response data
Dim oXMLHTTP ' this is the object that will request the data
const URLBase = "http://localhost:16370/api/" ' here is where my local web api was running
const AppSinglePath = "application/get/1" ' and this is just one of the paths available in my api

Next is a method that sets up my objects, and makes sure the api is ready to talk to us

Sub GetResponse
    Set oXMLHTTP = CreateObject("Microsoft.XmLHttp") ' create my request object
    Set oXMLDoc = CreateObject("MSXML2.DOMDocument") ' create my response object

    oXMLHTTP.onreadystatechange = getref("HandleStateChange") ' mode on this below, but it makes sure the API is ready
    Dim url = URLBase & AppSinglePath ' set up the URL we are going to request
    call oXMLHTTP.open("GET", url, false)
    call oXMLHTTP.setrequestheader("content-type","application/x-www-form-urlencoded")
    call oXMLHTTP.send()
End Sub

Now we need a method that will make sure the API is ready, and when it is, handle it. To understand the different ReadyState options check this link, but 4 is the only one we care about (request finished and response is ready)

http://www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp

Sub HandleStateChange
    If oXMLHTTP.readyState = 4 Then
        ' get the response
        Dim szResponse: szResponse = oXMLHTTP.responseText
        ' turn it into XML we can read
        call oXMLDoc.loadXML(szResponse)
        If oXMLDoc.parseError.errorCode <> 0 Then
            ' there was an error, tell someone
            call msgbox(oXMLDoc.parseError.reason)
        Else
            ' i was writing the response to a local file, because I wanted to see the XML
            Set oFile = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\APIResults\api.txt",2,true)
            oFile.WriteLine(oXMLDoc.xml)
            oFile.Close
            Set oFile = Nothing

            ' We need to make a query to dive into the XML with
            Dim strQuery = "/Application/ApplicationName"

            ' Now we need to rip apart the XML, and do whatever we want with it
            Set colNodes = oXMLDoc.selectNodes(strQuery)
            For Each objNode in colNodes
                WScript.Echo objNode.nodeName & ": " & objNode.text
            Next
        End If
    End If
End Sub

And for completion, here is a cleaned up version of the XML my API was returning

<Application>
    <ID>1</ID>
    <ApplicationName>MyApplication</ApplicationName>
</Application>

You can test different API calls by changing the second part of the path, and the strQuery that dives into the XML

mmeasor
  • 459
  • 3
  • 19