Errors Trying to Assign a User to an Issue

J.Iacutone May 16, 2024

Hi,

 

I have been trying to use the Jira API to create a ticket and assign a user to it. I can easily create the ticket, add details - description, issue type, summary etc. I can even assign a reporter.

 

But, when I try to assign an assignee, I get Invoke-RestMethod : {"errorMessages":["Invalid request payload. Refer to the REST API documentation and try again."]}

 

My code is:

$jira_url = 'https://cmcmarkets-sandbox-687.atlassian.net'
$jira_username = '<email address>'
$jira_api_token = '<my personal token>'


$base64_credentials = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($jira_username + ':' + $jira_api_token))

$url_path = '/rest/api/2/issue'

$headers = @{
Authorization = "Basic " + $base64_credentials
Accept = 'application/json'
'Content-Type' = 'application/json'
}

$requestBody = @{
fields = @{
assignee = @{
accountId = "<my accountId>"
}

}
}

 

$parameter = @{
URI = $jira_url + $url_path + "/<issue Id>/assignee"
Method = "PUT"
Body = ConvertTo-Json $requestBody -Depth 100
}

Invoke-RestMethod @parameter -Headers $headers

 

 

I have already collected the Issue ID and my user Id. They are used in place of the markers in the powershell code above.

 

Now, when I use a similar script, only with slightly different Body parameters, I can create the Issue easily enough. I then get its issue number and try to assign an assignee to it using https://{jira-api}.com/rest/api/2/issue/{issueid}/assignee (with the Body section as shown in the script), as shown in most online documents.

 

Just keep on getting:

Invoke-RestMethod : {"errorMessages":["Invalid request payload. Refer to the REST API documentation and try again."]}
At D:\Documents\test\JiraOcto_v03.ps1:136 char:1
+ Invoke-RestMethod @parameter -Headers $headers
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

 

Just cannot seem to understand why this error is occurring.

1 answer

1 accepted

2 votes
Answer accepted
Bo Zhao
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.
May 16, 2024

Hi @J.Iacutone 

Since we are already using the Assign Issue request URL, we just need to specify the accountID, so the request body can be as simple as.

$body = 

 {

"accountId": "<your accountId>"

}

REQUESTBODY;

 

J.Iacutone May 16, 2024

Hi @Bo Zhao 

Thank you for your prompt reply.

That did work for me.

 

Just from another angle, can a user be assigned when a ticket/issue is being created via the API?

tried adding

$AssignRequestBody = @{
fields = @{
project = @{
key = "ITSM"
}
summary = "[Octopus Deploy] Release"
description = "123test"
issuetype = @{
name = "CI/CD Change Request"
}
assignee = @{
accountId = "<my account Id>"
}
}
}

 

However, it does not work and gives an error:

Invoke-RestMethod : The remote server returned an error: (400) Bad Request.
At D:\Documents\test\JiraOcto_v04.ps1:46 char:1
+ Invoke-RestMethod @parameter -Headers $headers
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Bo Zhao
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.
May 16, 2024

Yeah, you can assign the issue during the creation of the Jira

Assuming you are using the POST /rest/api/2/issue API here

You can try adding the assignee as 

assignee = @{
id = "<my account Id>"
}

 You can also refer to the API guide here: https://developer.atlassian.com/cloud/jira/platform/rest/v2/api-group-issues/#api-rest-api-2-issue-post

J.Iacutone May 19, 2024

Hi Bo,

 

Thank you for your assistance.

It looks like the above will not work if the assignee option is not present in the main page, which its not. We have made the assignee option available in the main page now and we are able to assign an 'assignee' with

assignee = @{
id = "<my account Id>"
}

 Thank you again for your help and ideas.

Like Bo Zhao likes this

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
TAGS
AUG Leaders

Atlassian Community Events