Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Jira Invoke-RestMethod PowerShell 400 Bad Request

Wes Brown May 2, 2025

Have tried the $body_json output in Postman to grab the json data and the Jira is created without issue using the bearer token. I am also able to view a jira using the $headers variable which shows me my authorization is fine. I've tried different error capture methods and all I get back is 400 Bad Status and "ScriptStackTrace : at <ScriptBlock>, <No file>: line 2". I'm not even sure what that means. I've scoured these forums and google and am getting nowhere. Any assistance is much appreciated. Trying to avoid JiraPS module in favor of Invoke-RestMethod for future supportability.

 

$headers = @{

Authorization = "Bearer mytoken"

"Content-Type" = "application/json"

}

$restapiuri = "https://jira.mydomain.com/rest/api/2/issue"

$body = @{

    fields = @{

        reporter = @{

            name = "myname"

        }            

        project = @{

            key = "IT"

        }

        issuetype = @{

            name = "Task"

        }

        summary = "Test Issue 2"

        description = "Detailed description of the issue"

components = @(

            @{name = "MyTeam"}

        )

     }

}

$body_json = $body | ConvertTo-Json -Depth 5

Try{

Invoke-RestMethod -Uri $restapiuri -Method POST -Body $body_json -Headers $headers

 }

Catch {

    Write-Host "StatusCode:" $_.Exception.Response.StatusCode.value__ 

    Write-Host "StatusDescription:" $_.Exception.Response.StatusDescription

    }

 

Here's the json from $body_json



{
"fields": {
"issuetype": {
"name": "Task"
},
"description": "Detailed description of the issue",
"reporter": {
"name": "myusername"
},
"summary": "Test Issue 2",
"components": [
{
"name": "myteam"
}
],
"project": {
"key": "IT"
}
}
}

 This returned (201) created in Postman and I verified the Jira was created.

3 answers

1 accepted

0 votes
Answer accepted
Wes Brown May 5, 2025

I'm actually happy to report back that my script worked exactly as I had posted, but the user I was using didn't have permissions to create that specific issue type. Turns out Postman had cached my first attempt using my login credentials, even though I was explicitly specifying a bearer token. When I switched to a "triage" issue type that the user did have permissions to, the issue created successfully with no other changes.

0 votes
Wes Brown May 5, 2025

.

0 votes
Sanam Malleswari
Community Champion
May 4, 2025

Hi @Wes Brown

You're very close — your script is correctly forming the request, and the authorization is working fine since you're able to retrieve issues. The 400 Bad Request error usually means that Jira doesn't like something in the request body (even if the same JSON works in Postman).

Here are a few things to check:

  1. The reporter field — many Jira Cloud instances don’t allow setting the reporter via API unless the user has the right permissions. Try removing or commenting out the reporter block and see if the request succeeds.

  2. Component name casing — Jira is case-sensitive with some field values, so make sure MyTeam exactly matches the component name in your project.

  3. Error output — I've attached a revised script that gives you the full error response from Jira, which should help you pinpoint the issue more clearly.

let me know the output if the issue persists. I'm happy to help debug further!

Sanam Malleswari
Community Champion
May 4, 2025

Remove the reporter field altogether if it's not necessary
Try below
reporter = @{
accountId = "your-account-id-here"
}

Sanam Malleswari
Community Champion
May 4, 2025

# Jira REST API endpoint for issue creation
$restapiuri = "https://jira.atlassian.net/rest/api/2/issue"

# Define the issue fields
$body = @{
fields = @{
project = @{
key = "LEAR" # Replace with your project key
}
issuetype = @{
name = "Task" # Replace with your issue type
}
summary = "Create Issue via Rest API powershell"
description = "Detailed description of the issue"
components = @(
@{ name = "MyTeam" } # Replace with your component name
)
# Optional: add 'reporter' only if your Jira instance supports it
# reporter = @{
# name = "myusername" # Replace with a valid username
# }
}
}

# Convert body to JSON (with enough depth to include nested structures)
$body_json = $body | ConvertTo-Json -Depth 5

# Output the JSON for debugging
Write-Host "Request JSON:" -ForegroundColor Cyan
Write-Host $body_json

# Try to send the request
Try {
$response = Invoke-RestMethod -Uri $restapiuri -Method POST -Body $body_json -Headers $headers -ContentType "application/json"
Write-Host "`n✅ Issue created successfully!" -ForegroundColor Green
Write-Host "Issue Key: $($response.key)"
}
Catch {
$resp = $_.Exception.Response
$reader = New-Object System.IO.StreamReader($resp.GetResponseStream())
$responseBody = $reader.ReadToEnd()

Write-Host "`n❌ Failed to create issue" -ForegroundColor Red
Write-Host "StatusCode: $($resp.StatusCode.value__)"
Write-Host "StatusDescription: $($resp.StatusDescription)"
Write-Host "Jira Response:" -ForegroundColor Yellow
Write-Host $responseBody
}

Wes Brown May 5, 2025

Thanks, Sanam.  I'm actually happy to report back that my script worked exactly as I had posted, but the user I was using didn't have permissions to create that specific issue type. Turns out Postman had cached my first attempt using my login credentials, even though I was explicitly specifying a bearer token. When I switched to a "triage" issue type that the user did have permissions to, the issue created successfully with no other changes.

Sanam Malleswari
Community Champion
May 5, 2025

Thanks for the update! Glad to hear the script worked as expected

Suggest an answer

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

Atlassian Community Events