Missed Team ’24? Catch up on announcements here.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Powershell Invoke-WebRequest (500) Internal Server Error

SysAdmin Guy June 27, 2022

I am trying to create a Service Request in JIRA Service Desk and keep getting a 500 internal server error. If I run the script with GET, then I receive output with no errors. If I use POST, then I see the errors below. I think it has to do with the format of either brackets or maybe other needed fields.

As a test, I have not created any custom fields so that I can just get this to initially work with something simple (project,issuetype,summary,description).

I have used JiraPS which works well but for the environment I am working in, I either have to run Invoke-WebRequest or curl. I am stumped, any ideas would be greatly appreciated.

GET works below but if I comment and comment out for POST, it does not.

$user = 'testuser'
$pass = 'testpass'
$pair = "$($user):$($pass)"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
$basicAuthValue = "Basic $encodedCreds"
$Headers = @{
'Authorization' = $basicAuthValue
'Content-Type'='application/json'
'Accept' = 'application/json'
}

$body = @{
   fields = @{
     project = @{
     key = "HELP"
     }
     issuetype = @{
       name = "Service Request"
     }
     summary = "Test summary"
     description = "Test description"
     }
}

# $response = Invoke-WebRequest -Uri "http://testserver:8080/rest/servicedeskapi/request" -Headers $Headers -Method GET -Body $body
$response = Invoke-WebRequest -Uri "http://testserver:8080/rest/servicedeskapi/request" -Headers $Headers -Method POST -Body $body
$response.Content | ConvertFrom-Json

Get output:

size : 1
start : 0
limit : 50
isLastPage : True
_links : @{base=http://testserver:8080; context=; self=http://testserver:8080/rest/servicedeskapi/request?fields=System.Collections.Hashtable}
values : {@{_expands=System.Object[]; issueId=10161; issueKey=HELP-67; requestTypeId=1; serviceDeskId=1; createdDate=; reporter=; requestFieldValues=System.Object[]; currentStatus=; _links=}}

POST output:

Invoke-WebRequest : The remote server returned an error: (500) Internal Server Error.
At C:\Users\username\Desktop\PS_JSD_Invoke-WebRequest.ps1:26 char:13
+ $response = Invoke-WebRequest -Uri "http://testserver ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

Also, for the body, I have tried:

$body =@{
project = "HELP"
issuetype = "Service Request"
summary = "Test ticket"
description = "Test description"
}

Thank you!

 

1 answer

1 accepted

1 vote
Answer accepted
Radek Dostál
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 27, 2022

As per https://docs.atlassian.com/jira-servicedesk/REST/3.6.2/#servicedeskapi/request-createCustomerRequest I'm seeing a different body requirement, with the example of

{ 
"
serviceDeskId": "10",
"
requestTypeId": "25",
"
requestFieldValues": {
"summary": "Request JSD help via REST",
"description": "I need a new mouse for my Mac"
}
,
"
requestParticipants": [
"john"
]
}

 

Still it would be nice if Jira replied with something describing the problem rather than a straight up 500, but anyhoo I assume this is the cause.

SysAdmin Guy June 27, 2022

Thanks Radek, yeah, I came across that page as well. I think for it to work properly, for PowerShell, there has to be "=" instead of ":" and different spacing as well. I have also seen examples where at the beginning of some of the "{" there is an "@" character.

Linux side, I have all of this working with no issues via curl but for this, I am stuck with using PowerShell.

Radek Dostál
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 27, 2022

What I meant was different body data - the example from Atlassian contains different values. The 2 different bodies you mentioned don't have any of those fields.

 

Are you sure that the cURL uses the exact same body data as PS? It shouldn't work with either cURL or PS if you are passing just fields [project, issuetype, summary, description] - these sound like regular jira issue (NOT a service desk request type).

So either in cURL you are using different body; OR a different api endpoint. It looks like a body structure for https://docs.atlassian.com/software/jira/docs/api/REST/8.22.4/#issue-createIssue which is not a request type, just a regular issue.

SysAdmin Guy June 27, 2022

I am now trying to get the serviceDeskId and requestTypeId fields working now in Windows as that is listed in the curl for Linux. I am, however, using the same endpoint which is http://testserver:8080/rest/servicedeskapi/request (SSL after I get this working first). The only other required fields I have are summary and description. 

Linux (working)

 -d '{
"serviceDeskId": "1",
"requestTypeId": "1",
"requestFieldValues": {
   "summary": "Test summary",
   "description": "Test description"
    }
}'

Windows

$body = @{
    "serviceDeskId" = "1",
    "requestTypeId" = "1",
    "requestFieldValues" = @{
       "summary" = "Test summary",
       "description" = "Test description"
       }
}

Thanks

Radek Dostál
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 27, 2022

A lot of examples I see on the net include ConvertTo-Json (https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/convertto-json?view=powershell-7.2)

 

As in 

Invoke-WebRequest .. -Body ($params|ConvertTo-Json) .. 

 

So perhaps that could be a missing part, since your body is technically not a valid json strictly speaking, it might be worth a shot.

 

Other than that if you can tail your {{jira_home}}/log/atlassian-jira.log file the 500 should throw a stacktrace in there with hopefully some clue as to why it failed.

SysAdmin Guy June 27, 2022

Ok, I'll give that a try. Thanks again for the assistance.

SysAdmin Guy June 28, 2022

With the tips that you gave me, I was able to figure out what the issue was; I also used Invoke-RestMethod instead since I do not need as much data returned back. If others are interested, this is the entire script in which I edited the layout as well (without custom fields).

# Server information
$ServiceDeskID = "1"
$RequestTypeId = "1"
$Server = "http://testserver:8080"

# Authentication
$user = "testuser"
$pass = "testpass"

# Convert the username + password into a Base64 encoded hash for basic authentication
$pair = "$($user):$($pass)"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
$basicAuthValue = "Basic $encodedCreds"
$headers = @{
'Authorization' = $basicAuthValue
'Content-Type'='application/json'
'Accept' = 'application/json'
}

# Data to send
$body = @{
    "serviceDeskId" = 1
    "requestTypeId" = 1
    "requestFieldValues" = @{
      "summary" = "Test summary"
       "description" = "Test description"
       }
}

$jsonbody = $body | ConvertTo-JSON

# POST data to create a request
$URL = "$Server/rest/servicedeskapi/request"
$Response = Invoke-RestMethod -Uri $URL -Headers $headers -Method POST -Body $jsonbody
$Response.values

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events