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?
Thanks!
EDIT: code sample according to answer
$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";
}
}
}
Use Invoke-RestMethod to consume REST-APIs. Save the JSON to a string and use that as the body, ex:
$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
Here's the problem.
$restapiuri = " https://baseurl/rest/api/2/issue/ "
should be
$restapiuri = " https://baseurl/rest/api/2/issue "
After this change, it finally worked! The extra / is something PowerShell doesn't seem to enjoy :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.