Using API To Create Alert, Get a "Body Should be a JSON Object 400 error
Asked by Dave Menconi:

I'm trying to create an alert in Opsgenie. I created an api integration and I'm using the Geniekey from that integration.

I'm actually using postman but postman very helpfully outputs a cURL format and that's easier to paste in here:   
curl --location --request POST '<https://api.opsgenie.com/v2/alerts>' \\  
--header 'Content-Type: application/json' \\  
--header 'Authorization: GenieKey \<api integration\>' \\  
--header 'Host: api.opsgenie.com' \\  
--data-raw '{"message": "test message"}'

Result: 500 bad request  
{  
"message": "Body should be a JSON object",  
"took": 0.0,  
"requestId": "\<hash\>"  
}

Presumably this means that the body of the message isn't JSON but it certainly is. According to the documentation (<https://docs.opsgenie.com/docs/alert-api>) the only required field is message so that's all I added.

In my experience the problem will be trivial and obvious but I just can't see it in this case.

Please help me out and tell me what's wrong here!

Thanks

Dave


Answers:
author: Nick H
body: Hi [@Dave Menconi](/t5/user/viewprofilepage/user-id/4543864) ,

It could be an issue with the headers or data? This curl command seemed to work for my instance:

```
curl -X POST https://api.opsgenie.com/v2/alerts -H "Content-Type: application/json" -H "Authorization: GenieKey XXXXXXXXXXXX" -d '{"message": "An example alert message", "alias": "Life is too short for no alias", "description":"Every alert needs a description"}'
```

![curl1.jpg](https://community.atlassian.com/t5/image/serverpage/image-id/235962i5F103044F503A848/image-size/large?v=v2&px=999 "curl1.jpg")

![curl2.jpg](https://community.atlassian.com/t5/image/serverpage/image-id/235961i4596E2D0E0E125AC/image-size/large?v=v2&px=999 "curl2.jpg")

comments: author: Dave Menconi
body: Thanks so much for trying that, Nick!   
I execute your curl command just as you had it EXCEPT for using ++my++ GenieKey.

I got the same error with slightly more information (because I'm using curl instead of postman, I think):

{"message":"Invalid JSON","took":0.0,"requestId":"XXXXXX"}curl: (3) URL using bad/illegal format or missing URL  
curl: (3) URL using bad/illegal format or missing URL  
curl: (3) URL using bad/illegal format or missing URL  
curl: (3) unmatched close brace/bracket in URL position 44:  
description:Every alert needs a description}'  
\^

It seems to believe that the closing brace is not a brace ("}").

The question is, *why does it work for you but not for me?*   

Just thinking out load here...

We're using the same end point and the same headers and the same body. The ONLY difference is the GenieKey.   

I got this key by creating a custom API integration for this specific alert. I am NOT using the API key manager (that got me a different error) but I'm also not using the "Default API" integration.

Is that the wrong way to do it?   
Dave


author: Nick H
body: Hi [@Dave Menconi](/t5/user/viewprofilepage/user-id/4543864) ,

That is the correct way of using the Alert API. The GenieKey needs to be from an API integration, and**not** from the *API key management* tab.

The body's fields typically use double quotes, and the entire body needs to be enclosed by single quotes. Maybe that has something to do with it?

I get an "Invalid JSON" response if I remove a double quote from the body. The only field that is required to create an alert is the Message, so maybe start with only this field and see if you can generate an alert. Body for that might just be something like:

```
-d '{"message": "An example alert message"}'
```

or

```
curl -X POST https://api.opsgenie.com/v2/alerts -H "Content-Type: application/json" -H "Authorization: GenieKey XXXXXXXXXX" -d '{"message": "An example alert message"}'
```


author: Nick H
body: Feel free to share the body, your entire syntax, or a screenshot of the tests. We could review that as well and provide some feedback, suggestions, etc. Just be sure to remove any GenieKeys or sensitive information before sharing.


author: Dave Menconi
body: Basically you HAVE the body; I got it from you, after all. I'm stuck but I've been stuck before (often). This doesn't feel like an API problem, it feels like a configuration or server problem.   
I'm on a chat with OpsGenie support (and they seem to be baffled, too).   

I'll post back here when I figure it out.


author: Dave Menconi
body: I don't know why the curl command doesn't work. I suspect it's something to do with how I'm pasting it into the command window (I'll probably try it on a linux box and it will probably work).   

The problem I was having with postman (and the Python code I derived from it) was that I was trying to SET the host header. Note to self: don't set the host header.   

If left to itself Postman (and curl and python requests) will set the host header all on it's own. Setting that was the error. The error code was just bogus.   

Thanks for your help. Sorry that this didn't turn out to be a particularly useful community post. You win some you lose some.

Dave


Tags: api

cloud

integration

rest-api