Reply To: Calling a web service from tcl.

Clovertech Forums Read Only Archives Cloverleaf Cloverleaf Calling a web service from tcl. Reply To: Calling a web service from tcl.

#58593
Mike Shoemaker
Participant

    Hey Todd,

      Here is the package that connects and sends data to a webservice. Basically, you call this with 3 parameters:

    inData is the data (string) to be sent to the webservice

    inUrl is the url of the webservice

    inParam is the parameter used in the url of the webservice.

    The statement set query2 [::http::formatQuery $queryParam $xml_data] will gather the data string and the parameter and format a query sting.

    The statment set http [::http::geturl $url -query $query2] will do the work basically and post the data to the url supplied using the properly formated query created in the statement above.

    The rest of the code is for handling the responses. There are 2 types, the HTTP response which is from the web server (an example is a 404 not found error when requesting a page) and the HTML error which is the response from the webservice code (this is where your xml response would be)

    Code:

    package require http
    package require WebServiceAckNak

    proc webServiceConn { inData inUrl inParam } {

     set html “”
     set url $inUrl
     set xml_data $inData
     set queryParam $inParam

     set query2 [::http::formatQuery $queryParam $xml_data]

     if {![catch {set http [::http::geturl $url -query $query2]} error_msg ]} {

       set html [::http::data $http]

       upvar #0 $http status

         foreach index [array names status] {

           switch -exact — $index {
             querylength { set query_len [array get status $index] }
             currentsize { set response_len [array get status $index] }
             http { set http_status [array get status $index] }
             default { }
           }
         }

       set http_status [lindex $http_status 1]
     
           set ackNak [webServiceAckNak $http_status $html]

           return $ackNak  

     } else {

       echo “error has been encountered::$error_msg”

     }

           if {$html != “”} {
             [::http::cleanup $html]
           }
    }

    The second package i have handles the server and html responsed.

    Code:

    proc webServiceAckNak { inHTTP inHTML } {

     set result “”

     set httpResponse $inHTTP
     set htmlResponse $inHTML

       set htmlResponseString [lindex [split $htmlResponse n] 1]

         regexp >(.*?) $htmlResponseString match result

           if {$result == “working”} {

             return 0
           
           } else {

             echo $htmlResponse
             echo $httpResponse

           }

    }

    To configure this in the engine, i use a raw route for the messages i want to send and configure the proc on the outbound tps.  The outbound tps proc contains all the translation for the message and then at the end i call the webservice proc as:

    Code:


    set res [webServiceConn $msg $url $queryParameter]
     if {$res == 0} {
       set status_cond 1
       echo “Message Sent Successfully.”
     } else {
       set status_cond 0
      echo “An error has been encountered with the webservice connection. Retrying…COUNT: $counter”
     }

    Let me know if you need more details with the code, i’m happy to help.

    Have fun!

    Mike