I have being tryng to create schedules automatically via python but I cannot, I have already been able to get the data but not to post, I am using the documentation which does not help much. When I try to post i receive the error 405, any suggestion?
Hi @Octavio Ornelas ,
would you please share the code with us, so we can help you with it? Thank you.
import requests
from requests.structures import CaseInsensitiveDict
from pandas import DataFrame, DateOffset, to_datetime
url = <url of the schedule>
headers = CaseInsensitiveDict()
headers["Authorization"] = "GenieKey <API KEY>"
headers["content-Type"] = "application/json"
resp = requests.post(url, headers = headers, json = <body os the schedule>)
print(resp.status_code)
BODY of the schedule
{'name': 'Factory Support - Space On-Call Schedule',
'description': 'On-call Schedule for Space Segment (Time Zone is EET)',
'timezone': 'Europe/Helsinki',
'enabled': True,
'ownerTeam': {'name': 'Factory Support - Space'},
'rotations' :{
'name': 'Week 30',
'startDate': '2022-07-25T15:00:00Z',
'endDate': '2022-07-31T07:00:00Z',
'type': 'weekly',
'length': 1,
'participants': [{'type': 'user',
'id': '60a0f278-cb62-4809-800d-ed5e7f483159'}],
'timeRestriction': {'type': 'weekday-and-time-of-day',
'restrictions': [{'startDay': 'monday',
'endDay': 'tuesday',
'startHour': 18,
'endHour': 10,
'startMin': 0,
'endMin': 0},
{'startDay': 'tuesday',
'endDay': 'wednesday',
'startHour': 18,
'endHour': 10,
'startMin': 0,
'endMin': 0},
{'startDay': 'wednesday',
'endDay': 'thursday',
'startHour': 18,
'endHour': 10,
'startMin': 0,
'endMin': 0},
{'startDay': 'thursday',
'endDay': 'friday',
'startHour': 18,
'endHour': 10,
'startMin': 0,
'endMin': 0},
{'startDay': 'friday',
'endDay': 'monday',
'startHour': 18,
'endHour': 10,
'startMin': 0,
'endMin': 0}]}}}
I get the response 405 I was thinking that maybe the body has an error but the format is exactly the same as the one shown in the documentation
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thank you @Octavio Ornelas . According to the documentation, the POST request needs to be send to https://api.opsgenie.com/v2/schedules, but you are using schedule URL, so maybe your URL is not right?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
According to the documentation error 422 means "Semantic errors in request body"
In JSON there should be double quotes instead of single quotes:
{ "name": "ScheduleName", "description": "ScheduleDescription", "timezone": "Europe/Kirov", "enabled": true, "ownerTeam": { "name": "ops_team" }, "rotations": [ { "name": "First Rotation", "startDate": "2017-02-06T05:00:00Z", "endDate": "2017-02-23T06:00:00Z", "type": "hourly", "length": 6, "participants": [ { "type": "user", "username": "user@opsgenie.com" }, { "type": "none" }, { "type": "user", "id": "ac463592-dbd2-4ca3-alp1d-48fhf5j5c871" } ], "timeRestriction": { "type" : "weekday-and-time-of-day", "restrictions" : [ { "startDay" : "monday", "startHour" : 8, "startMin" : 0, "endDay" : "tuesday", "endHour" : 18, "endMin" : 30 }, { "startDay" : "wednesday", "startHour" : 8, "startMin" : 0, "endDay" : "thursday", "endHour" : 18, "endMin" : 30 } ] } } ] }
Would you please try to change it?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Octavio Ornelas Are you able to get also error message what's wrong with the request body, not only error code?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
it says --rotations is invalid which i assume means tht the dict rotations has a non expected value, this is the dict i am using
"rotations" :{
"name": "Week 30",
"startDate": "2022-07-25T15:00:00Z",
"endDate": "2022-07-31T07:00:00Z",
"type": "weekly",
"length": 1,
"participants": [{"type": "user",
"username": "<user_name"}],
"timeRestriction": {"type": "weekday-and-time-of-day",
"restrictions": [{"startDay": "monday",
"endDay": "tuesday",
"startHour": 18,
"endHour": 10,
"startMin": 0,
"endMin": 0},
{"startDay": "tuesday",
"endDay": "wednesday",
"startHour": 18,
"endHour": 10,
"startMin": 0,
"endMin": 0},
{"startDay": "wednesday",
"endDay": "thursday",
"startHour": 18,
"endHour": 10,
"startMin": 0,
"endMin": 0},
{"startDay": "thursday",
"endDay": "friday",
"startHour": 18,
"endHour": 10,
"startMin": 0,
"endMin": 0},
{"startDay": "friday",
"endDay": "monday",
"startHour": 18,
"endHour": 10,
"startMin": 0,
"endMin": 0}]}}}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yes, it means something is wrong with this part of JSON, but I cannot tell what exactly. Please try to experiment a little bit with it to find it out.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I have already found the bug but I have another issue when I am trying to update the schedule I cant do it I have the response 409
<Response [405]> {'message': "Request method 'POST' not supported", 'took': 0.001, 'requestId': 'c7a2aff3-3c27-4551-b248-e33c8bab846e'}
The code is the same iI just changed the id of the schedule to be updated in the url as the documentation says
url = "https://api.opsgenie.com/v2/schedules/1743f25-c16b-42e0-b017-3a8277410fda"
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Octavio Ornelas ,
according to the documentation you need to send PATCH request (not POST) to update the schedule.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi! I was able to do it by using post, there was a mistake in the dictionary I was using, fortunately the bug was found
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.