Create JIRA Issue Powershell via REST API

Dan_Witter
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
May 15, 2020

Hi, 

 

I'm getting a bad request 400 error when I try to create an issue. I've used postman to test the JSON and that works fine. I've caught the error and I get this:

 

{"errorMessages":[],"errors":{"summary":"Field 'summary' cannot be set. It is not on the appropriate screen, or unknown.","description":"Field 'description' cannot be set. It is not on the ap
propriate screen, or unknown.","assignee":"Field 'assignee' cannot be set. It is not on the appropriate screen, or unknown.","priority":"Field 'priority' cannot be set. It is not on the appro
priate screen, or unknown."}}

 

 

This is my script below. I've changed the credentials etc for privacy. 

 


function ConvertTo-Base64($string) {
$bytes = [System.Text.Encoding]::UTF8.GetBytes($string)
$encoded = [System.Convert]::ToBase64String($bytes)
return $encoded;
}

function Get-HttpBasicHeader([string]$username, [string]$password, $headers = @{}) {
$b64 = ConvertTo-Base64 "$($username):($password)"
$headers["Authorization"] = "Basic $b64"
$headers["X-Atlassian-Token"] = "nocheck"
return $headers
}

 

 

$url = "https://URL.com/rest/api/2/issue"
$headers = Get-HttpBasicHeader "username" "password?"

$body = ('
{
"fields":
{
"project":
{
"id": "1234"
},

"summary" : "This is the Summary",

"description" : "This is the issue description",

"issuetype":
{
"id": "3"
},

"assignee":
{
"name": "name"
},

"priority":
{
"id": "5"
}
}

}
')
try{
Invoke-RestMethod -Uri $url -Headers $header -Method Post -ContentType "application/json" -Body $body -Verbose
}
catch
[System.Net.WebException] {$exception = $_.Exception
$respstream = $exception.Response.GetResponseStream()
$sr = New-Object System.IO.StreamReader $respstream
$errorResult = $sr.ReadToEnd()
Write-Host $errorResult
}

 

 

Thanks

1 answer

0 votes
Antonio Piazza
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
December 5, 2024

I know this is old, but I have almost the same issue.  While debugging my Powershell script I copy my JSON body variable value. I can curl that exact JSON to JIRA API using the same JSESSIONID cookie that my PowerShell script uses and create an issue as intended. Still, for some reason, I get the same error that you are getting when my PowerShell script makes the API request via Invoke-WebResquest and Invoke-RestMethod.  I know my Cookie has the proper permissions and the screen has the fields I am trying to populate. Have you solved this problem?

Suggest an answer

Log in or Sign up to answer