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

Powershell Invoke-RestMethod returns 500 "Unexpected character"

Rune Rasmussen January 26, 2021

Preface:

I am very new to Powershell and webrequests. I may miss something very obvious, or have not understood the solutions others have posted in questions about similar issues.

Purpose:

Access to our Atlassian instance is controlled through groups in Azure AD.

I have a small script that will create a group for a new project or Confluence space in AAD.
I want to extend this script to also create the new project/space, and have a "one stop shop" script.

I want this to work in Powershell, in order to simplify the excecution of scripts for those who will be using it.

Code:

$user = [System.Text.Encoding]::UTF8.GetBytes("MY-EMAIL:MY-TOKEN")
$headers =@{
Authorization="Basic " + [System.Convert]::ToBase64String($user)
Accept="application/json"
}
$projectTypeKey = "service_desk"
$projectTemplateKey = "com.atlassian.servicedesk:itil-v2-service-desk-project"
$Projectname = "Ninja Project"
$body =@{

"key" = $key
"name" = $ProjectName
"leadId" = "MY-ID"
"projectTypeKey" = $projectTypeKey
"projectTemplateKey" = $projectTemplateKey

}
$uri = "https://MYSITE.atlassian.net/rest/api/3/project"

Invoke-RestMethod -Uri $uri -Method POST -Headers $headers -Body $body -ContentType "application/json"

 

Error:

Invoke-RestMethod: {"statusCode":500,"message":"org.codehaus.jackson.JsonParseException: Unexpected character ('k' (code 107)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: org.apache.catalina.connector.CoyoteInputStream@1c316782; line: 1, column: 2]"}

 

Additional info:

The unexpected character changes from time to time, and has shown as "('p' (code 112))".
I cannot force the unexpected character to change, but trying in Powershell ISE, Powershell 7, and Visual Studio Code (With PS 7.1) will often give a different unexpected character.

The unexpected character remains constant in the three different environments, but will differ between them.

 

The POST request works just fine in Postman.

1 answer

1 accepted

0 votes
Answer accepted
Andy Heinzer
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
January 28, 2021

Hi Rune,
I see that you're using Powershell in order to try to create a project in Jira Cloud via the REST API. I think I found out what is happening here. In my view, I believe that your POST call is failing here because Powershell is not creating a true JSON payload here for Jira Cloud to process as expected for that (POST /rest/api/3/project) endpoint.

I did a lot of trial and error to find something that works with Powershell here. I should note that I did this in a Windows 10 machine, with Powershell 5

PS C:\Users\IEUser> (Get-Host).Version

Major Minor Build Revision
----- ----- ----- --------
5 1 17763 316


One other thing to note: In my testing I substituted the headers a bit in order to supply my own encoded credentials. If you try my code below and this does not work, I would suggest first trying to do a REST API GET call to Cloud instead, just to make sure that it's not an authentication problem (/rest/api/issue/{IssueKey} is a common endpoint).

 

$user = [System.Text.Encoding]::UTF8.GetBytes("MY-EMAIL:MY-TOKEN")
$headers =@{
"Authorization"="Basic " + [System.Convert]::ToBase64String($user)
"Accept"="application/json"
"Content-Type"="application/json"
}

$projectTypeKey = "service_desk"
$projectTemplateKey = "com.atlassian.servicedesk:itil-v2-service-desk-project"
$ProjectName = "Ninja Project"
$body =@{
key = "NINJA"
name = $ProjectName
projectTypeKey = $projectTypeKey
projectTemplateKey = $projectTemplateKey
leadAccountId = "123456:000000000-a000-0000-0000-0000000000"
assigneeType = "PROJECT_LEAD"
avatarId= 10200
}
$uri = "https://[examplesitename].atlassian.net/rest/api/3/project"
$jsonbody = $body | ConvertTo-JSON

Invoke-RestMethod -Method POST -Uri $uri -Headers $headers -Body $jsonbody


I tweaked a number of things from your original post, for example

  • the value for $key was not defined,
  • we need to use 'leadAccountId' for Cloud today instead of LeadId,
  • I put some header names in quotes,
  • and I moved one of your headers into the $headers variable.

But the big hurdle for me when testing this was that instead of trying to pass that existing $body value, I created a new $jsonbody variable, and called the Powershell utility ConvertTo-JSON in order to make sure that the payload was formatted properly.  I think this will be necessary for any POST/PUT call to Jira Cloud that requires a JSON payload

Try this and let me know if you run into any problems.
Cheers,
Andy

Rune Rasmussen February 2, 2021

Hello Andy

I added your changes to my code, and I now have my Ninja Project in our DEV instance.
It worked just fine in Powershell 7.1

Thank you very much for the help!

//Rune

Rune Rasmussen February 2, 2021

One weird thing I notice though, is that for Confluence it works just fine without having "Accept"="application/json" and "Content-Type"="application/json" in the Headers, and without using the "ConvertTo-JSON" parameter for the Body.

Suggest an answer

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

Atlassian Community Events