JIRA Powershell Create Issue via REST API

Joshua Denenberg February 19, 2021

I saw the older posting but I'm having issues regardless creating an issue via powershell and even follow those steps/fixes get errors. My script looks like the following:

 

function CreateNewJiraIssue($summary, $description, $issue_type)
{
$headers = @{
Authorization = 'Basic ' +
[Convert]::ToBase64String(
[Text.Encoding]::UTF8.GetBytes("jira_user_name:$jira_auth_token")
)
}

$payload = @
"fields"= @{
"project"= @{
"key"= $JIRA_PROJECT
};
"summary"= $summary;
"description"= $description;
"issuetype"= @{
"name"= $issue_type
}
}
}
$response = Invoke-RestMethod -uri $JIRA_URL -Headers $headers -Method POST -ContentType "application/json" -Body ($payload | ConvertTo-Json)
}

The $JIRA_URI is "'https://********.atlassian.net/rest/api/2/issue".

Using postman, my body works correctly. I have tried both passing the dict directly and convertto-json and similarly get a 400 error.  Creds appear good (no 401 unless I mess with them).

I'm deeply confused as this works perfectly with python.requests but converting it to ps errors out.

1 answer

0 votes
Andy Heinzer
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 22, 2021

Hi Joshua,

I understand that you are trying to create issues to Jira Cloud using Windows Powershell, but appear to be getting an error in doing so.   I think I can see some tweaks that might help here.  First, I think that you might be missing a bracket for the inital payload, but I also tweaked this a bit in my own testing to do the ConvertTo-JSON and store that into a different variable. Also I removed the double quotes around the field names, but included quotes around the values.

I find this separate step for the conversion to JSON can be helpful in trying to troubleshoot and see what is actually getting passed to Jira Cloud.

What I had ended up with was essentially:

function CreateNewJiraIssue($summary, $description, $issue_type)
{
$headers = @{
Authorization = 'Basic ' +
[Convert]::ToBase64String(
[Text.Encoding]::UTF8.GetBytes("jira_user_name:$jira_auth_token")
)
}

$payload = @{
fields= @{
project= @{
key= "A1"
};
summary= "test123"
description= "123test"
issuetype= @{
name= "Bug"
}
}
}
$jsonpayload = $payload | ConvertTo-JSON
Invoke-RestMethod -Method POST -Uri https://[Cloudsite].atlassian.net/rest/api/2/issue -Headers $headers -ContentType "application/json" -Body $jsonpayload

This works in my own testing, but obviously you will want to adjust this for the variables you want to pass in that function rather than the hard-coded values I entered here.  But if this isn't immediately helpful, it might help to add a line to end such as

echo $jsonpayload

In order to see the exact JSON payload that is being sent in that POST call.  Extra quotes in the payload, or missing quotes might be sending an unexpected payload.

 

Try this and let me know the results.  If you are still getting stuck, post the jsonpayload output if you feel comfortable doing so, perhaps we can learn more here by looking at the output.

Andy

Suggest an answer

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

Atlassian Community Events