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!
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
What would be the code to
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
facing error. Can anyone help me ? https://community.atlassian.com/t5/Jira-questions/Create-JIRA-Issue-with-REST-API-via-PowerShell/qaq-p/2531309#M597707
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.