Forum Discussion

chicks's avatar
chicks
Regular Contributor
13 years ago

testing curl webservices



I've just gotten the go-ahead to start automating our curl webservice calls.  I'm hoping there's some way to do this without starting curl up as a separate application and parsing the command lines.



Has anybody worked with curl requests & responses in TestComplete?
  • You can create an JScript HTTP client in a similar way to creating an AJAX client in (an old) IE browser, e.g.:




        function GET (URL) {

            var XmlHttpRequest = Sys["OleObject"]("MSXML2.XMLHTTP.3.0");

            XmlHttpRequest.open("GET", URL, false);

            XmlHttpRequest.setRequestHeader("Content-Type", "application/json");  

            XmlHttpRequest.send();

            try {

                return XmlHttpRequest.responseText;

            } catch (e) {

                return "Error";

            }

        }


    This example makes a synchronous call to a URL, expecting a JSON response and returning it as a string. You can adapt this basic idea for different content types, POSTs, etc. I don't think it's possible to write this as asynchronous since TestComplete doesn't have the browser's event loop.
  • chicks's avatar
    chicks
    Regular Contributor
    Thank you Nick !   I'll give it a shot when I get back to it.



    Regards,  Curt
  • chicks's avatar
    chicks
    Regular Contributor


    For others purposes, I've added what I ended up with.

    1) I'm using the ActiveXObject rather than the Sys("OleObject) as Nick suggested.



    Does anybody have a comment on when to use ActiveX...  versus Ole.... ?



    2) I modified the GET code to support PUT or POST with form data, since some of the webservices use that.



    3) The curl specific parameters are NOT needed when constructing the XmlHttpRequest.  I imagine this is a "No DUH !" for everybody with any familiarity at all with this stuff, but came as an epiphany to me.

    =====================================================

    function http_request (URL, method, formData) {

            var XmlHttpRequest = new ActiveXObject("MSXML2.XMLHTTP.3.0");

                    

            if ( ( method == undefined) || (method == "") ) {

               method = "GET";

            }

            

            // XmlHttpRequest.open( Method, URL, Asynchronous, UserName, Password)

            // method e.g. GET, POST, HEAD, PUT, DELETE, OPTIONS...

            // Asynchronous = true, do not wait on a server response,  false, pause current execution utnil the request is complete

            XmlHttpRequest.open(method, URL, false);

            // Name, Value

            XmlHttpRequest.setRequestHeader("Content-Type", "application/json");  

            XmlHttpRequest.setRequestHeader("access_token",  access_token_variable_or_constant);  

            XmlHttpRequest.send(formData);

            try {

                return XmlHttpRequest.responseText;

            }

            catch (e) {

                return "Error" + e ;

            }

    }



    =================

    Thanks Again Nick!   Regards,  Curt Hicks