Forum Discussion

tpoirier's avatar
tpoirier
Contributor
13 years ago

Http post request example



I have a node.js script that I would like to convert and run in TestComplete 9, looking for some direction as to how to get a http post/get set up and maybe an example?



Thanks,

T.



//node.js file

var http = require('http');

var querystring = require('querystring');

var post_data = JSON.stringify({ Message: 'This is a test' }); //JSON.stringify({ userId: 'jdoe', password: 'tesTing1$', rememberMe: true }); //JSON.stringify({ AccountNumber: '', FirstName: '', LastName: '', EmailAddress: '' });

 

 /*

 DEV OPTIONS

 */

 var options = {

  hostname: 'localhost',

  port: 59573,

  path: '/Service1.svc/Test/',

  method: 'POST',

  headers: {

   'Content-Type': 'application/json; charset=utf-8'

   ,'Content-Length': post_data.length

  }

 };


var req = http.request(options, function(res) {

  console.log('STATUS: ' + res.statusCode);

  console.log('HEADERS: ' + JSON.stringify(res.headers));

  res.setEncoding('utf8');

  res.on('data', function (chunk) {

   var responseObj = JSON.parse(chunk);

   

   console.log('Message: ' + responseObj.Message);

   console.log('BODY: ' + chunk);



   console.log('path: ' + options.path);

   

  });

 }).on('error', function(e) {

  console.log('problem with request: ' + e.message);

 });

 

 req.end(post_data);