Create a Jira project from Powershell with REST API

Thijs Gerrits January 3, 2020

I am trying to create a new project in Jira from Powershell using REST API.

I have the following code, but it is giving me http error 400. I can't figure out where the problem is, most likely in the json part, but how to pinpoint the exact error?

$npuri = "$baseurl/rest/api/2/project"
$npjson = "{    `"key`": `"MNew`",
    `"name`": `"M Software Sample`",
    `"projectTypeKey`": `"business`",
    `"projectTemplateKey`": `"com.atlassian.jira-core-project-templates:jira-core-project-management`"
,    `"description`": `"Example Project description`"
    `"lead`": `"userid`",
    `"assigneeType`": `"PROJECT_LEAD`",
    `"avatarId`": 10262,
    `"permissionScheme`": 10342,
    `"notificationScheme`": 10000,
    `"categoryId`": 10120
    }"
$response=Invoke-RestMethod -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -ContentType "application/json" -Uri $npuri -Body $npjson -Method POST

A basic query using same base URL and authentication works.

1 answer

0 votes
Leonard Chew
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 3, 2020

I don't know where the problem is, but here are some ideas to narrow down the problem:

  • use a different client (for isntance a browser plugin) to "play" with your rest-service
  • wrap your json body in single-quotes, then you dont need to escape your double quotes
  • use a simpler use-case (something like create issue) to start with before creating a project, as to test your body syntax
Thijs Gerrits January 6, 2020

Hi,

Thanks for your suggestions. Using postman, I was able to clear up the JSON syntax, mostly by removing all unnecessary fields.

I also changed the way the JSON body was constructed:

$hash = @{    key"KEY";
    name"TG Software Sample";
    projectTypeKey"software";
    projectTemplateKey="com.pyxis.greenhopper.jira:gh-scrum-template";
    description"Example Project description";
    lead"userid";
    assigneeType"PROJECT_LEAD"
    }

$json = $hash | ConvertTo-Json

 After that, a project was created successfully on Jira.

 

Regards,

 

Thijs

Suggest an answer

Log in or Sign up to answer