Global Event Rule api error - unsupported media type

rest-api
event-rules
(Shaun Mills) #1

I am working on a python script to create global event rules using pagerduty’s rest api v2 and am running into some issues just trying to get started.

################ create-eventrule.py #################

Response Status: 415
b’{“error”:“Unsupported Media Type”}’

Here is my data payload:

Data payload

data = {“condition”: [“and”,[“contains”,[“path”,“payload”,“source”],“website”]], “actions”: [[“route”,“test”],[“suppress”,False],[“severity”,“info”]]}

(Thomas Roach) #2

If you could please send us the script you’re trying to use, we’d be happy to take a look at it and help determine what the issue is.

(Shaun Mills) #3

Here is the script I have thus far

#! /usr/bin/python3

import requests


authorization_token = '##########################'
pagerduty_session = requests.Session()
pagerduty_session.headers.update({
  'Authorization': 'Token token=' + authorization_token,
  'Accept': 'application/vnd.pagerduty+json;version=2'
})

# PagerDuty API Endpoint for event rules
url = 'https://api.pagerduty.com/event_rules'

# Data payload
data = {"condition": ["and",["contains",["path","payload","source"],"website"]], "actions": [["route","test"],["suppress",False],["severity","info"]]}
print(data)
print(type(data))

# Response
response = pagerduty_session.post(url, data=data)

print("\n################ create-eventrule.py #################\n")

# Print status and body of response
print('Response Status:', response.status_code)
if response.status_code != 200:
    print(response.content)
    raise SystemExit(1)
(Jay Chiarella) #4

Hi Shaun,

The original response error indicates a 415 HTTP error.

This usually indicates an unsupported media type was sent in the API request, as the linked guide mentions:

The format problem might be due to the request’s indicated Content-Type or Content-Encoding

Our Global Event Rules documentation indicates that you must include Content-Type: application/json in the API request, which I saw missing in your API request.

Could you please see if you receive the 415 error after adding the Content-Type header.

Cheers,

(Shaun Mills) #5

That seems to have helped now I think i just need to adjust the payload i am sending

{'condition': ['and', ['contains', ['path', 'payload', 'source'], 'website']], 'actions': [['route', 'P2KGCOP'], ['suppress', False], ['severity', 'info']]}
<class 'dict'>

################ create-eventrule.py #################

Response Status: 400
b'{"error":"Bad Request"}'

updated code

#! /usr/bin/python3

import requests


authorization_token = '##########################'
pagerduty_session = requests.Session()
pagerduty_session.headers.update({
  'Authorization': 'Token token=' + authorization_token,
  'Content-Type': 'application/json',
  'Accept': 'application/vnd.pagerduty+json;version=2'
})

# PagerDuty API Endpoint for event rules
url = 'https://api.pagerduty.com/event_rules'

# Data payload
data = {"condition": ["and",["contains",["path","payload","source"],"website"]], "actions": [["route","P2KGCOP"],["suppress",False],["severity","info"]]}
print(data)
print(type(data))

# Response
response = pagerduty_session.post(url, data=data, headers=pagerduty_session.headers)

print("\n################ create-eventrule.py #################\n")

# Print status and body of response
print('Response Status:', response.status_code)
if response.status_code != 200:
print(response.content)
raise SystemExit(1)
(Thomas Roach) #6

Let us know if we can be of further assistance!

(Malcolm Konner) #8

Hi Shaun,

A payload could be as simple as follows:

"condition": [
                "or",
                [
                    "exists",
                    [
                        "path",
                        "headers",
                        "from",
                        "0",
                        "address"
                    ]
                ]
            ],
            "catch_all": false,
            "advanced_condition": [],
            "actions": [
                [
                    "route",
                    "[PUT_SERVICE_ID_HERE"
                ],
                [
                    "suppress",
                    true
                ],
                [
                    "annotate",
                    "meow"
                ]
            ]
        }, 
1 Like
(Shaun Mills) #9

Thanks Malcom that helps a lot!

(Shaun Mills) #10

Ok guys the trick was changing the request parameters

response = pagerduty_session.post(url, json=data2, headers=pagerduty_session.headers)

Instead of using the data paramater, I used the json parameter for the requests.post() method which did create the global event rule!

1 Like
(Jade Paoletta) #11

Hi Shaun,

Happy to hear that did the trick! Feel free to let us know if you have any other questions.

Best,

#12

Malcolm, does this section need to be formatted differently “catch_all”: false,
I get the following error - NameError: name ‘false’ is not defined

#13

Is there a way to order the rule when it is created? Ex. Add it to the top of the existing rules?

#14

Figured it out. false needs to be False…case sensitive.

(system) closed #15

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.