Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,298,687
Community Members
 
Community Events
165
Community Groups

Cannot close OpsGenie Alert via PS Invoke-RestMethod, "422 Unprocessable Entity"

Edited

I can get an alert successfully created with this PS scriptlet:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$body = @{   
"priority"="P1";   
"message"="TESTING JIMMY example alert message";   
"alias"="testingjimmy";   
"description"="TESTING JIMMY Every alert needs a description"   
}
   
$url="https://api.opsgenie.com/v2/alerts?apiKey=xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"    I
nvoke-RestMethod
-Method Post -ContentType "application/json" -Uri $url -Body (ConvertTo-Json $body)

But, I get "422 Unprocessable Entity" with every single option I chose (id, tiny, alias) in the below with proper modifications:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12    
$header = @{       
"Authorization"="GenieKey "+[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx"))       
}
       
$url="https://api.opsgenie.com/v2/alerts/testingjimmy/close?identifierType=alias"       
Invoke-RestMethod -Method Post -ContentType "application/json" -Header $header -Uri $url

I've tried RequestIDs and the Tiny#s (alert#) too, with zero luck.  The API service that this is tied to in the back end has the proper settings to close the alert as well.

Any ideas?  I assume

I used several references, this is the one I used the most:

https://docs.opsgenie.com/docs/alert-api

 

 

2 answers

I think I finally got it.  Although the API documentation clearly state that the BODY is not mandatory, it is.  I also needed to convert how I was passing the Authorization variable, I don't need to convert it before passing it.  Here is what finally worked:

To Create:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12$body = @{    "priority"="P1";    "message"="TESTING JIMMY example alert message";    "alias"="testingjimmy";    "description"="TESTING JIMMY Every alert needs a description"    }$url="https://api.opsgenie.com/v2/alerts?apiKey=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"Invoke-RestMethod -Method Post -ContentType "application/json" -Uri $url -Body (ConvertTo-Json $body)

To Close:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12$header = @{    "Authorization"="GenieKey xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";    }$url="https://api.opsgenie.com/v2/alerts/testingjimmy/close?identifierType=alias"$body = @{    "user"="Jimmy";    "source"="TESTING JIMMY example alert message";    "note"="closed by script";    }Invoke-RestMethod -Method Post -ContentType "application/json" -Header $header -Uri $url -Body (ConvertTo-Json $body)
0 votes
Samir Atlassian Team May 17, 2019

Hi Andrew - It looks like you are passing the URL, and the Headers in the Close request. Could you try including a body in that request too, like you're doing in the Create request? Something similar to this:Alert_API.png

I tried, still same 422 Unprocessable Entity.  I also looked at the variable list for the body in closing the alerts, but they do not match what is required for the alert creation:

user, source and note are the body options for close.

Here is an additional error when I try to navigate to the destination through a browser:

{"code":406,"message":"Could not find acceptable representation","took":0.0,"requestId":"618268d4-efd3-4c34-8722-168a56e936d6"}

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Confluence

An update on Confluence Cloud customer feedback – June 2022

Hi everyone, We’re always looking at how to improve Confluence and customer feedback plays an important role in making sure we're investing in the areas that will bring the most value to the most c...

219 views 1 4
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you