Forum Discussion

tpoirier's avatar
tpoirier
Contributor
12 years ago
Solved

Setting up a POST request

Hi, I am trying to test a REST API using the TestComplete 9. I have looked at the example on downloading a site and built the following simple test. My question come as I try to pass in the login details, is there a way to something similar like JSON.stringify here to pass the data to the server. As it stands now, i receieve a error code of 500 (internal server error) and have the API developer looking into it on his end. Next step will be to try something like fiddler to capture the data but I am looking for some direction or if anyone out there has any thoughts or ideas. I have alreadly also seen the example on downloading a google doc and haven't been able to gain anyhting from that. 



Thanks




function connect(){


  var webURL = "myURLhere";


  var post_data = {SerialNumber: '1234', Login: 'test', Password: '111111111'};


  


  var objHTTP = new ActiveXObject("MSXML2.XMLHTTP");


  objHTTP.open("POST", webURL, false);


  objHTTP.setRequestHeader("Content-Type", "application/json; charset=utf-8");


  objHTTP.setRequestHeader("Content-Length", post_data.length);


  objHTTP.send(post_data);


  


  while((objHTTP.readyState != 4) && (objHTTP.readyState != 'complete')) {


    Delay(100);


  }


  


  if(200 != objHTTP.Status) {


    Log.Message("Returned Status Code of: " + objHTTP.Status);


    Log.Message("Status Text: " + objHTTP.StatusText);


    return;


  }


  


  if(200 == objHTTP.stauts) {


    Log.Message("Returned Status Code of: " + objHTTP.Status);


    Log.Message("DATA: " + objHTTP.ResponseBody);


  }


  


}

  • I think the simplest way is to create json string yourself. It should look like this:

    var post_data_str = "{\"SerialNumber\":\"1234\",\"Login\":\"test\",\"Password\":\"111111111\"}"

    I used the Firefox Web Console to get this string from your object using JSON.stringify call.



    If you need to post many different json requests then you can get stringify code here: https://github.com/douglascrockford/JSON-js/blob/master/json2.js

2 Replies

  • I think the simplest way is to create json string yourself. It should look like this:

    var post_data_str = "{\"SerialNumber\":\"1234\",\"Login\":\"test\",\"Password\":\"111111111\"}"

    I used the Firefox Web Console to get this string from your object using JSON.stringify call.



    If you need to post many different json requests then you can get stringify code here: https://github.com/douglascrockford/JSON-js/blob/master/json2.js