Create JIRA Issue with REST API via PowerShell

Deleted user May 3, 2019

I'm trying to create a JIRA issue via Powershell.

Here's my code.

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

}


$restapiuri = "https://baseurl/rest/api/2/issue/"
$headers = Get-HttpBasicHeader "user" "password"


$body
= (' { "fields": { "project": { "id": "10402" }, "summary": "Test", "description": "Test", "duedate": "2019-05-11", "issuetype": { "id": "3" }, "reporter": { "name": "user" }, "priority": { "id": "10101" }, "customfield_11403": "Test", "security": { "id": "11213" }, "components": [ { "id": "10805" } ] } }') Invoke-RestMethod -uri $restapiuri -Headers $headers -Method POST -ContentType "application/json" -Body $body

The JSON part of it runs fine as I've tried it using Postman and the issue got created.

In Powershell however, I always get a 400 bad request returned. Does anyone got any ideas why this might be?

The authentication also works because with the same code I'm able to add comments and retrieve filter details etc. I'm about to lose my mind. I just can't get this working.

Thanks!

 

2 answers

1 accepted

1 vote
Answer accepted
Stephen Sifers
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 7, 2019

Hello Marius,

Thank you for sending over your scripts along with the response code you’re receiving. From reviewing your script I noticed there was an additional slash (/) within your $restapiuri variable which may make PowerShell angry (sometimes).

Outside of this, the 400 response typically means there is a field missing or misrepresented within the JSON data. Since you reported it works within postman then this shouldn’t be the case. The next step we should take would be to see the actual response from the Invoke-restmethod. You’ll want to incorporate the usage of

-ErrorAction
#and/or
-ErrorVariable

within your Invoke-restmethod to have your actual response with error message stored for viewing later. Atop of this, you’ll also want to use a catch statement to ensure you’re able to view the error when they are triggered. Here are reference documents to help achieve this:

I hope this proves helpful in finding your actual response error with the message to find what the cause of the failure is.

Regards,
Stephen Sifers

Deleted user May 8, 2019

Stephen, I was just moments away from pulling my hair out. I spent hours trying to find the issue and in the end, the slash was in fact the culprit......

Thank you so much for pointing that out! It now works! :D

Like Stephen Sifers likes this
0 votes
Srecko Anzic April 29, 2021

What would be the code to 

Create JIRA Issue with REST API via PowerShell?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events