HttpClientRequest returns unwanted message along with responce

Not applicable

Hi all,

I have the below code :

loadLibrary("xtk:common.js"); loadLibrary("xtk:shared/json2.js"); loadLibrary('xtk:shared/nl.js'); loadLibrary("xtk:shared/js.js"); //loadLibrary("crm:common.js"); var OK_RESPONSE = "OK"; var ERROR_RESPONSE = "ERROR"; var membershipIdValue = 195200; var organizationIdValue = 0; var paymentFrequencyValue = "Month"; var effectiveDateValue = "2016-06-24"; var promoPackageCodeValue = "P56S"; var jsonInput = { "membershipId": membershipIdValue, "organizationId": organizationIdValue, "paymentFrequency": paymentFrequencyValue, "effectiveDate": effectiveDateValue, "promoPackageCode": promoPackageCodeValue }; var http = new HttpClientRequest("https://acm.sit.internal.int.com.au:20443/api/campaignmanager/CalculateRate"); http.method = "POST"; http.header["Content-Type"] = "application/json"; http.header["Accept"] = "application/json"; //writeLog(DEBUG, 'registrationinvite', 0, JSON.stringify(subscriber)); http.body = JSON.stringify(jsonInput); try{ logInfo('Before TRY'); logInfo('After TRY'); http.execute(); var response = http.response; //writeLog(INFO, 'registrationinvite', 0, response.code + ': ' + response.body.toString()); logInfo(response.code); logInfo('Ready 2 executed'); logInfo(response.body.toString()); if( response.code == 200){ var result = JSON.parse(response.body.toString()); logInfo('result'+result); } }catch(e){ logInfo('Catched some error'); var code = http.response.code; var body = http.response.body.toString(); // if we get here there was an error try{ // writeLog(ERROR, 'registrationinvite',0, 'Exception caught generating url. response code: ' + code + //logInfo('Exception caught generating url. response code: ' + code + '; response body: ' + body + "; " + e); }catch(x){ //logInfo('error logging error: ' + x); //writeLog(ERROR, 'registrationinvite',0, 'error logging error: ' + x); } } // if we get here there was an error // writeLog(ERROR, 'registrationinvite',0, 'Error generating url'); //logInfo('TEST generating url');

and i am getting the below response :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/> <title>401 - Unauthorized: Access is denied due to invalid credentials.</title> <style type="text/css"> <!-- body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;} fieldset{padding:0 15px 10px 15px;} h1{font-size:2.4em;margin:0;color:#FFF;} h2{font-size:1.7em;margin:0;color:#CC0000;} h3{font-size:1.2em;margin:10px 0 0 0;color:#000000;} #header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS", Verdana, sans-serif;color:#FFF; background-color:#555555;} #content{margin:0 0 0 2%;position:relative;} .content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;} --> </style> </head> <body> <div id="header"><h1>Server Error</h1></div> <div id="content"> <div><fieldset> <h2>401 - Unauthorized: Access is denied due to invalid credentials.</h2> <h3>You do not have permission to view this directory or page using the credentials that you supplied.</h3> </fieldset></div> </div> </body> </html> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd"> <HTML><HEAD><TITLE>Not Authorized</TITLE> <META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD> <BODY><h2>Not Authorized</h2> <hr><p>HTTP Error 401. The requested resource requires user authentication.</p> </BODY></HTML> {"membershipId":195200,"organizationId":0,"paymentFrequency":"Month","effectiveDate":"2016-06-24T00:00:00","promoPackageCode":"P56S","rebatePercentage":17.861,"rateDetails":[ {"rateType":0,"rateDescription":"CurrentRate","rebate":"Y","amount":123.45}, {"rateType":1,"rateDescription":"PromoRate","rebate":"Y","amount":155.75}, {"rateType":2,"rateDescription":"PromoRateDiff","rebate":"Y","amount":32.30}, {"rateType":3,"rateDescription":"NonRebateCurrentRate","rebate":"N","amount":150.3}, {"rateType":4,"rateDescription":"NonRebatePromoRate","rebate":"N","amount":189.65}, {"rateType":5,"rateDescription":"NonRebatePromoRateDiff","rebate":"N","amount":39.35}]}; SyntaxError: JSON.parse

I should be seeing only the response :

{"membershipId":195200,"organizationId":0,"paymentFrequency":"Month","effectiveDate":"2016-06-24T00:00:00","promoPackageCode":"P56S","rebatePercentage":17.861,"rateDetails":[ {"rateType":0,"rateDescription":"CurrentRate","rebate":"Y","amount":123.45}, {"rateType":1,"rateDescription":"PromoRate","rebate":"Y","amount":155.75}, {"rateType":2,"rateDescription":"PromoRateDiff","rebate":"Y","amount":32.30}, {"rateType":3,"rateDescription":"NonRebateCurrentRate","rebate":"N","amount":150.3}, {"rateType":4,"rateDescription":"NonRebatePromoRate","rebate":"N","amount":189.65}, {"rateType":5,"rateDescription":"NonRebatePromoRateDiff","rebate":"N","amount":39.35}]};

Please advice what is wrong ..

0 1 176
1 REPLY 1

robinm
New Member

Hey @shiva

Are you still working on this .. did you solve the problem ?

I am unable to test a solution as this URL is blocked for external access, but can offer a suggestion:-

Focus on the 401 response and try to solve that.

  • Do you perhaps need to send an Authorization header with the request?
  • Is there some other authorization requirement, such as a client certificate to be present?