Cannot use Responder or Tags in API to Create an ops genie alert
Asked by Dietrich Lehr:

Using powershell to create an alert in OpsGenie via the API, and I keep getting "The remote server returned an error: (422) Unprocessable Entity"  

This only happens when I attempt to use the tags or responders fields in the body. If I leave those out, I can successfully create alerts. What am I doing wrong with the formatting??  

<br />

```
$body = @{            
message     = "MESSAGE TITLE HERE"            
description = "DESCRIPTION HERE"            
priority    = "P5"            
alias       = "XXXXXXX"            
tags        = "tag"            
responders  = {                
    name = "TEAMNAMEHERE"                
    type = "team"            
}        
} | ConvertTo-Json
```

If I remove the tags and responders fields, this works perfectly.


Answers:
author: Allen Barnard
body: Hi good morning,

It would appear that tags and responders are being defined as incorrect data types, "tags" should be an array that accepts a list of values, while responders should be an array that accepts a list of objects.

It also would appear that you are creating a lot of unnecessary work for yourself by first constructing the payload $body as a PsObject then converting it to a JSON. The most efficient method would be to store your JSON (in a JSON format) and store it in $body as a literal string, please see my example below. Please note that no conversion is needed $body already contains a valid JSON

```
$body = '{
"message": "MESSAGE TITLE HERE",
"description":"DESCRIPTION HERE",
"alias": "XXXXXXX",
"priority":"P5",
"responders":[{"id":"TEAMNAMEHERE", "type":"team"}],
"tags": ["Tag1","Tag2"]
}'
```

comments: author: Dietrich Lehr
body: I've formatted tags as an array with @{"tag1", "tag2"}. I prefer the convert-to method to keep my syntax uniform throughout my scripts.  

I'll give the JSON formatting a try and let you know if that works.


author: Dietrich Lehr
body: This worked! Strange the convertto-json didn't format the request correctly.  

One other question...how do I insert a variable? I have a list of servers I want to pass as a note.  

adding this works just fine...but looks like it tries to read my variable as a string and just sends the variable's name.  

```
"note": "TEXT"
```


author: Allen Barnard
body: Hi Dlehr, my apologies for delayed response I have been on annual leave, could you please provide an example of how you are constructing the payload so I can test it in my lab?


author: Dietrich Lehr
body: ```


$body = '{
    "message": "servers",
    "description": "server list",
    "alias": "xxx",
    "priority": "P5",
    "responders": [
        {            
            "id": "myteam",
            "type": "team"
        }
    ],
    "tags": [
        "tag1", "tag2"
    ],
    "note": $MyVariable}'
}'


```

<br />

This will only return the actual string saying $MyVariable even though its a list of servers


author: Allen Barnard
body: Hi Dlehr,

Since we are constructing the JSON as a literal string in Powershell, you will need to use string concatenation to input dynamic variables as part of the constructed literal string.

This payload works in my lab.

```
?$body = ('{
"message": "servers",
"description": "server list",
"alias": "xxx",
"priority": "P5",
"responders": [
{ 
"id": "myteam",
"type": "team"
}
],
"tags": [
"tag1", "tag2"
],
"note": "' + $MyVariable + '"
}')
```

Please note that the literal string is now contained between an opening **(** and a closing **)**Please also note how I have encapsulated the variable within double-quotes within the literal string.

```
"' + $MyVariable + '"
```


author: Dietrich Lehr
body: ```
$body = @{
            message     = "servers"
            description = "list of servers"
            priority    = "P5"
            alias       = "XXXX"
            note        = $MyVariable
} | ConvertTo-Json


```

Previously, I used this format which worked except when I attempted to use the responders and tag fields. I was able to send my variable (list of servers) using this format before and it displayed correctly by displaying the servers in the $myvariable powershell object.

```


Microsoft.PowerShell.Commands.Internal.Format.FormatStartData Microsoft.PowerShell.Commands.Internal.Format.GroupStartData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData
```

Using the literal JSON format method as you showed up there, it does try to send an object this time instead of just the variable name, but it's more like the object typename and not the object itself. The above is what's shown in the opsgenie notes section using your formatting above.  

I'd prefer not to have to use pure JSON syntax and have my original $body formatting work where I convertto-json...but using responder and tag fields i would get an error from opsgenie


author: Allen Barnard
body: Hi Dlehr good morning,

Can you please provide an example of the $body PsObject with Responders and tags included in the object. Can you also provide an output of that $body after it has been piped to ConvertTo-Json that way you can view the converted JSON in your shell. I would like to see the how ConvertTo-Json is constructing the JSON so I can run a validation check to make sure it compliant with RFC 8259 JSON standard.

Please note we only support JSON paylaods that compliant with RFC xxxx JSON standards, please further note that we only support JSON formats we do not support the conversion of JSON from other objects such as PsObject.

Please see the screenshot of a working example of the syntax that I provided my previous message, as you can see the API accepts the request with a HTTP status of 202 and returns a JSON which confirms the request will be processed.

![PowershellExample.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/84602i629A9E7D10FD607B/image-dimensions/733x320?v=v2 "PowershellExample.PNG")

And here is the resulting Alert in OpsGenie, with responders assigned and the $MyVariable value string correctly displayed in the notes.

![ResultingAlert.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/84605i2FE035C7395DFA93/image-dimensions/694x358?v=v2 "ResultingAlert.PNG")


author: Dietrich Lehr
body: ```
$Compare = Compare-Object -ReferenceObject $AllVMNames -DifferenceObject $TaggedVMsNames |
 Where-Object {$_.SideIndicator -eq "<="} |
 Select-Object -ExpandProperty InputObject |
 Sort-Object |
 Format-Table



#this is my new working syntax

$body = @{
            message     = "servers"
            description = "list of servers"
            alias       = "XXXX"
            priority    = "P5"
            responders  = @(
                @{
                    id      = "myteam"
                    type    = "team"
                }
            )
            tags        = @(
                "tag1"
                "tag2"
            )
            note        = $Compare
        } | ConvertTo-Json
```

```
#output from $compare

server1
server2
server3
server4
server5
```

<br />

![2020-02-27 13_19_20-? nsx_alert_no_tag.ps1 - powershell scripts - Visual Studio Code \[Administrator\].jpg](https://community.atlassian.com/t5/image/serverpage/image-id/84641iB62BA2E6FB1E0D51/image-size/large?v=v2&px=999 "2020-02-27 13_19_20-? nsx_alert_no_tag.ps1 - powershell scripts - Visual Studio Code [Administrator].jpg")

so I went back to the drawing board determined to make my powershell syntax work so I didn't have to deal with JSON and figured it out.  

However...I'm getting a 422 (unprocessable Entity) error from Opsgenie when I sub in $Compare for any string. It doesn't like the object being given.


author: Dietrich Lehr
body: So I made one other change that made this work. I piped $Compare \| Out-String. This only allowed it to get input in the Opsgenie notes section as a list of server names.

It still does not appear anywhere in the Jira Service Desk ticket despite a rule in the Opsgenie to JSD integration that would lead me to believe it should.   

![2020-02-27 16_25_39-Opsgenie - Integration.png](https://community.atlassian.com/t5/image/serverpage/image-id/84672i9361AAEE4F5FB7A4/image-size/large?v=v2&px=999 "2020-02-27 16_25_39-Opsgenie - Integration.png")


author: Allen Barnard
body: Hi Dlehr,

Glad you now have the add note working on Opsgenie, I will need access to your Opsgenie instance and logs. For security reasons for me to be able to request access to your logs I need you to raise a support ticket. The most convenient way for you to do this is to log into <https://app.opsgenie.com>, then once you are logged in use the chat bubble to start a chat this will automatically open a support ticket in our system. If you provide the Chat Support Engineer with a link to this community post you may request the chat be escalated directly to me or another Senior Support Engineer in your region.

If you prefer a more traditional support method than chat you can log into this page and support a ticket for the Opsgenie product <https://support.atlassian.com/contact/#/>

Thanks and kind regards,

Allen


author: Dietrich Lehr
body: Support has confirmed Opsgenie notes not appearing in Jira service desk ticket is an issue in their end and has elevated my issue