[SM9.32] Restful Response in Javascript



I have some problems with the RESTful response inside the HPSM.


I created a test function to generate a new Incident with the new implemented RESTful API.


If nothing goes wrong i got a JSON string with all information, which I can convert to an Object.

This is okay.


BUT, if i got an Error (Example: Invalid Validation) - I got an Header Response Code: 400 instead of 200.

But not more. (see Attachment - error-response-hpsm)


In the external REST Client ( I use POSTER - an Firefox Extension), I can see the full response for the

400 Error (see attachment - error-response-rest-client)



This is my test function:


var objConfig = {
	"url"		: "">localhost:23080/.../",
	"username"	: "<your_rest_username>",
	"password"	: "<your_rest_password>",
	"module"	: "incidents"

// remove the // below to get a success response
// currently we will get an 400 error, because 
// some required fields are not filled
var objCreate = {	
	"Incident" : {
		"AffectedCI" 		: "adv-nam-server-mail",
		"AlertStatus"		: "updated",
		"Area"				: "failure",
		"Category"			: "incident",
		"AssignmentGroup"	: "Network",
		//"Title"				: "My Rest Incident",
		//"Description"		: "My Rest Description",
		"Subarea" 			: "function or feature not working",
		"Service" 			: "E-mail / Webmail (North America)",
		"Impact" 			: "2",
		"Urgency" 			: "2"

createRecord( objCreate );

 * Create a new Incident with the RESTful API
 * @param   {Object} objRecord Object with all required fields
 * @return  {String}           JSON String with the response
function createRecord( objRecord ) {

	var JSON = system.library.JSON.json();
	//build header for basic authorization
	//without this, we will get an 401 error	
	var arrHeaders		= getRequestHeader();
	//build url for the request
	//Default Action for POST is "create" so we don't need
	//to add the action again
	var cRequestUrl		= objConfig.url objConfig.module;
	//convert the given object to an json string
	cPostBody			= system.library.JSON2.toJSON(objRecord);

	try {
		//lets run the the HTTP request
		//HTTP Command - url to execute - http header - POST Body
		var rcRequest 	= doHTTPRequest("POST", cRequestUrl, arrHeaders, cPostBody,30,30);
		//convert response json string back to an object
		var objResponse	= JSON.parse(rcRequest);
		//Object Keys are
		//* Messages (=Return Message like Incident IMXXX has been opened)
		//* ReturnCode (=0 for SUCCESS)
		//* Incident (=Created Incident Record - only with the fields from the Web Service Configuration)
		print("Messages: \n" objResponse.Messages.join("\n"));
		print("Return Code: " objResponse.ReturnCode);
		print("Incident Details: \n" objResponse.Incident.toSource());

	} catch( cError ) {
		//something goes wrong 
		//check also http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html 
		//to get the description of the given http response code like 401 etc.
		//currently it's not possible (or i don't know how) to get the correct
		//error response - for me it looks like that the HPSM has an filter
		//which removes the response body if the response header is not 200
		//if it's possible to use the reponse, we can use the same code
		//as above JSON.parse() etc.
		print("ERROR: \n" cError);

//Helper function to generate the HTTP Request header
//This function is required in each function which sends an doHTTPRequest
function getRequestHeader() {
	var authHeader		= new Header();
	authHeader.name		= "Authorization";
	authHeader.value	= "Basic " getBase64Encode(objConfig.username ":" objConfig.password);	
	//Content Type application/json is required
	//for all POST Actions - otherwise we will get an 501 error
	var typeHeader		= new Header();
	typeHeader.name		= "content-type";
	typeHeader.value	= "application/json";	
	var arrHeaders		= new Array();
	return arrHeaders;

//Helper function to generate the base64 code
//Base64Encode does not return a value value
//so we need this function as workaround
function getBase64Encode(input){
	var keystr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 /=";
	var output = "";
	var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
	var i = 0;
	while (i < input.length) {
  		chr1 = input.charCodeAt(i  );
		chr2 = input.charCodeAt(i  );
		chr3 = input.charCodeAt(i  );
		enc1 = chr1 >> 2;
		enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
		enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
		enc4 = chr3 & 63;
		if (isNaN(chr2)) {
			enc3 = enc4 = 64;
		} else if (isNaN(chr3)) {
			enc4 = 64;
		output = output  
		keystr.charAt(enc1)   keystr.charAt(enc2)  
		keystr.charAt(enc3)   keystr.charAt(enc4);
	return output;	


To use this function to have to change the url, username, password in the objConfig.



Has anyone an idea, how I can read and use the Response Body for responses != 200?

Or is it not possible at the moment to use this and I have to open an Support Ticket?



My Environment is:

* Windows 2008 Server

* HPSM 9.32.0016 (Client/Server)

* MSSQL Server






  • Hi,


    Dont know much about REST protocol but your path should be like




    Kindly refer to Web Services Guide which comes with SM.



    Abhijit K


  • Hi,



    if I want to use the SOAP api then http://<servername>:<serverport>/SM/7/IncidentManagement.wsdl is the correct url.


    But the RESTful API uses the following url: http://<servername>:<serverport>/SM/9/rest/incidents

    (as descriped in the SM9.32_WebServices.pdf)

  • Have you tried with a servername different than "localhost"? Probably the FQDN of the server?

    And what about the port? In the examples from the 9.32 WebServices guide port 14930 is used..

    I have researched and this looks like an issue with url access.

    Before executing it in Restful, make sure the url can be accessed from SM side.

  • Hi,


    i think you misunderstood me.


    I got a connection to the RESTful API (from HPSM side and from an external client) with localhost as servername.

    I don't know where you found the 14930 Port, in the webservice documentation which I have, there is no reference to this port, but anyway, the 23080 port in my case works fine.


    My problem is, that the response in an error case (example: invalid validation) returns a HTTP Header Code 400 instead of 200.


    And i can only read the complete Response in case of HTTP Header 200. All other HTTP Response message will not shown correctly (for me) - but i need also the response message in case of 400 or 401 to get an better output and for an better error handling.


    You know what i mean?



  • Verified Answer

    I found the solution.


    There is a ScriptLibrary "RestfulConstant". This Library has a function names "getHttpStatus".


    If I change the value for "badval" from 400 to 200 then I got my required JSON Code with the validation messages.

    This is not a really good way but as workaround good enough :)



    Maybe this helps some one, which has the same problems.

  • Hello,

    Can you please give some more details about how u achieved to get the messages because changing the "RestfulConstant" script library didn't work for us