Highlighted
Respected Contributor.. Respected Contributor..
Respected Contributor..
395 views

Troubleshoot/debug error updating Service Manager via REST API: (400) Bad Request

I'm attempting to update a value in Service Manager using a REST API, but when I attempt it I receive an error:

Exception calling "GetResponse" with "0" argument(s): "The remote server returned an error: (400) Bad Request."

Is there a way to get more information about what is causing this error or otherwise debug?

The code in question (username and password redacted for security reasons, but I've confirmed they're correct)

######################################
#Function to create the JSON object
######################################
function AddJsonToRequest {
 param($rq, $body);
 $bodyStream = $rq.GetRequestStream();
 $bodyWriter = New-Object System.IO.StreamWriter($bodyStream);
 $bodyWriter.Write($body);
 $bodyWriter.Close();
 $bodyStream.Close();
}

########################################
#Writing back to the HPSM Task record
########################################

$cred = "<username>:<password>";
$cred = [System.Text.Encoding]::GetEncoding("ISO-8859-1").GetBytes($cred);
$cred = [System.Convert]::ToBase64String($cred);
$url = ("{0}/SM/9/rest/xcelrequesttasks/{1}/action/Update" -f $baseHpsmUrl, $taskId);
$rq = [System.Net.WebRequest]::Create($url);
$rq.Method = "POST";
$rq.ContentType = "application/json";
$rq.Timeout = 100000;
$rq.ReadWriteTimeout = 100000;
$rq.Headers.Add("Authorization", "Basic " + $cred);

LogWrite ("URL: {0}" -f $url);
LogWrite ("rq: {0}" -f $rq);

if($finalStatus -eq "ok") {
 $xcelDataCollectYesNo="No";
 $ClosureCode = '1';
} else {
 $xcelDataCollectYesNo="Yes";
 $ClosureCode = '3';
}

$ClosureComments = [string]::Join(" ", $comments);
$ClosureComments = $ClosureComments -replace '"', ""
$ClosureComments = $ClosureComments -replace '\\', ""

$body = '{{ "RequestTask": {{ "xcelDataCollectYesNo":{0}, "ClosureCode":{1}, "ClosureComments":"{2}" }}}} ';
$body = ($body -f $xcelDataCollectYesNo, $ClosureCode, $ClosureComments );

AddJsonToRequest $rq $body
$rq.GetResponse();

0 Likes
1 Reply
Highlighted
Honored Contributor.
Honored Contributor.

Hello Mark

Thanks for contacting us.

For your problem/request, Service Manager has a parameter that can be inserted into the sm.ini or sm.cfg file to debug rest usage, the information will apear in the log file. i dont know what version you are using, but here is the document were this parameter is describe for SM9.41.

https://docs.microfocus.com/SM/9.41/Classic/Content/parameters/reference/parameter_debugrest.htm

BR

 

If you find that this or any other post resolves your issue, please be sure to mark it as an accepted solution.
If you are satisfied with anyone’s response please remember to give them a KUDOS by clicking on the STAR at the bottom left of the post and show your appreciation.
0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.