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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

JIRA Powershell Create Issue via REST API

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 Feb 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
Community showcase
Posted in Jira Software

Presenting the "Best of 2020" Jira Software roundup!

Catch up with Atlassian Product Managers in our 2020 Demo Den round-up! From Advanced Roadmaps to Code in Jira to Next-Gen Workflows, check out the videos below to help up-level your work in the new ...

7,193 views 8 28
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you