Missed Team ’24? Catch up on announcements here.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Using API To Create Alert, Get a "Body Should be a JSON Object 400 error

Dave Menconi January 23, 2023

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

1 answer

1 accepted

2 votes
Answer accepted
Nick H
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
January 23, 2023

Hi @Dave Menconi ,

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

curl2.jpg 

Dave Menconi January 23, 2023

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

Nick H
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
January 23, 2023

Hi @Dave Menconi ,

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"}'
Nick H
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
January 23, 2023

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.

Dave Menconi January 24, 2023

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. 

Dave Menconi January 24, 2023

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

Like Nick H likes this

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PERMISSIONS LEVEL
Site Admin
TAGS
AUG Leaders

Atlassian Community Events