Skip to main content
Kofax

How to call a Kapow Web Service from VB.NET

Summary

13564

Question:

How to call a Kapow RESTFul Web Service from a VB.NET code

Answer:

  1. Open Kapow Designer.
  2. Deploy a robot that returns a complex variable.
  3. Open the Kapow Management Console.
  4. Go to Repository > Robots and locate the robot.
  5. Click on the REST icon for that robot.
  6. Take note of the REST URL.
  7. Select the desired format for the request and copy the request string.
  8. Open Microsoft Visual Basic.
  9. Create a Web Form page with a button and a textbox
  10. Assign the code to call the service and to store the result in the textbox.

Here is a sample of this code:

    Protected Sub Btn_Sync_Click(sender As Object, e As EventArgs) Handles
Btn_Sync.Click
    REST_URL =
"http://ServerName:8080/ManagementCon...EST_Demo.robot"
'Enter your robot's REST URL
    REST_Request = 'this is an example where the pc name will be returned
        "{
            ""parameters"" : [ {
            ""variableName"" : ""restDemoINPUT"",
            ""attribute"" : [ {
                ""type"" : ""text"",
                ""name"" : ""INPUT_Info"",
                ""value"" : """ + Environment.MachineName + """
                } ]
             } ]
            }"
        REST_Response = ""
       
        'Invoke RESTFUL POST KAPOW Synchronous Dim JsonSring = REST_Request
        Dim myUri As New Uri(REST_URL)
        Dim data = Encoding.Default.GetBytes(JsonSring)
        REST_Response = Send_REST_Request(myUri, data, "application/json", "POST")
        'Send_REST_Request is documented below
        'Extract Result from XML
        Dim Xml As XmlDocument
        Xml = New XmlDocument
        Xml.LoadXml(REST_Response)
        Dim nodeList As XmlNodeList = Xml.GetElementsByTagName("attribute")
        For Each node As XmlElement In nodeList
            If node.Attributes(1).InnerText = "INPUT_Info" Then TextBox.Text =
              node.InnerText 'INPUT_Info is the variable returned by the web service.
        Next
    End Sub

    Private Function Send_REST_Request(uri As Uri, jsonDataBytes As Byte(), contentType
     As String, method As String) As String
        Dim req As WebRequest = WebRequest.Create(uri) req.ContentType = contentType
        req.Method = method
        req.ContentLength = jsonDataBytes.Length
        Dim stream = req.GetRequestStream()
        stream.Write(jsonDataBytes, 0, jsonDataBytes.Length) stream.Close()
        Dim response = req.GetResponse().GetResponseStream() Dim reader As New StreamReader(response)
        Dim res = reader.ReadToEnd()
        reader.Close()
        response.Close()
        Return res
     End Function

Note: if you are using Kapow Enterprise under Tomcat, you have to configure the service authentication to prevent having an error (401) Unauthorized:

  1. Open the Kapow Management Console.

  2. Go to Admin > Projects.

  3. Edit the project associated with your robot.

  4. Go to the Services tab

  5. Uncheck the "Authenticate REST/SOPA requests" checkbox. Note: when authentication is disabled, anyone with access to the application can invoke these services, so be careful when selecting which robots to put into this project.

  6. Optionally, you may fill in Access-Control-Allow-Origin to control which domains may call the service; you may use * to allow all.

Keywords: Kapow RESTful Web Service Synchronous Asynchronous, Kapow, Robotic Process Automation