cancel
Showing results for 
Search instead for 
Did you mean: 

XSJS - Outbound HTTP Request - POST

Former Member
0 Kudos

I try to send a record to HTTP destination (REST services) using POST method. execution of the code takes to else loop and return  "No Response: Issue with the Coding Part". Do let me know, if any part mentioned wrong in the code. Appreciate the help

Note: HTTP destination and xsjs file stored in same package

try {

    var query = 'SELECT "MATERIALNUMBER","DIVISION","CUST_GRP1" ' + 'FROM \"PVENKATA\".\"MATERIAL_PRICING\" where "MATERIALNUMBER" = \'XXXXX\'';

    var conn = $.db.getConnection();

    var pstmnt = conn.prepareStatement(query);

    var result = pstmnt.executeQuery();

 

  var client = new $.net.http.Client();

  var destination = $.net.http.readDestination("pkg-pvenkata.SIAL_XS1", "MATPRICE_CALL");

  var request =new $.net.http.Request($.net.http.POST, "result");

  client.request(request, destination);

  var response = client.getResponse();

  if(response.body){

   $.response.setBody(response.body.asString());

  }

  else {

   $.response.setBody( "No Response: Issue with the Coding Part" );

  }

  

  $.response.status = response.status;

  $.response.contentType = "application/json";

  }

  catch(x){

   $.response.status = 500;

   $.response.setBody( e.message );

   $.response.status = $.net.http.INTERNAL_SERVER_ERROR;}

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi,

You mentioned you tried to send a record to HTTP destination using POST method, unfortunately I did not find this in your code. I'm not sure if you want to send the result with this code line:

var request =new $.net.http.Request($.net.http.POST, "result");


If so, you cannot bind the result when you construct the Request object. You need to first parse the result set and then use the method setBody() to bind the record you want to send.


You can find the API here. JSDoc: Class: Request

Best regards,

Wenjun

Former Member
0 Kudos

Hello Wen,

Thanks for the reply.

I figure out the POST functionality using req.setBody(). But facing another set of issue this time. From the code, I get the list materials and passing the results to setbody(), unfortunately all query results are similar(same replica) and due to that i can see 1 record is got posted. Please let me know, if any issue with the code

var client = new $.net.http.Client();

var destination = $.net.http.readDestination("pkg-pvenkata.SIAL_XS1", "MATPRICE_CALL");

var query ='SELECT "MATERIALNUMBER","_BIC_BCATNUMB","DIVISION","CUST_GRP1","_BIC_BVMSTA","SALESORG","_BIC_BMTPOS","_BIC_BKONDM","_BIC_BTEMPB","DATETO","_BIC_BAMOUNT","CURRENCY","_BIC_BPRATA","_BIC_BMATBRAND","DATEFROM","STD_DESCR","_BIC_BPMATTIME","PMAT_SALES_CUST_GRP1","_BIC_BMSTAV","_BIC_BKSTBM_C","MAT_SALES","SALE_CHECK","_BIC_BMATBRAND_1" ' + 'FROM \"PVENKATA\".\"MATERIAL_PRICING1\"'; 

function close(closables)

var closable; 

var i; 

for (i = 0; i < closables.length; i++)

closable = closables[i]; 

if(closable)

closable.close(); 

}

function getMaterial()

  {

  var Materiallist = [];

  var connection = $.db.getConnection();

  var statement = null;

  var resultSet = null;

  try{

  statement = connection.prepareStatement(query);

  resultSet = statement.executeQuery();

  var Materialdetails = {};

  while (resultSet.next())

  { 

  Materialdetails.materialNumber = resultSet.getString(1);

  Materialdetails._BIC_BCATNUMB = resultSet.getString(2);

....................................................

...................................................

  Materiallist.push(Materialdetails);

  }

  }

  finally { 

  close([resultSet, statement, connection]);

  }

  return Materiallist;

  }

function doPOST(){

  try{

  var req =new $.net.http.Request($.net.http.POST,"/dataload/v1/materialprice/json");

  req.contentType = "application/json";

  req.setBody('{"materials":' + JSON.stringify(getMaterial()) + '}');

  client.request(req, destination);

  $.response.status = $.net.http.OK;

  $.response.setBody("Record Loaded");

  }

  catch(err){ 

                     $.response.contentType = "text/plain"; 

                     $.response.setBody("Error while executing query: [" + err.message + "]"); 

                     $.response.returnCode = 200;

  }

  }

  doPOST();

Former Member
0 Kudos

Fixed the issue.  Problem with the while loop.

Thanks for the support