Issue using API for creating Schedules automatically
Asked by Octavio Ornelas:

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?


Answers:
author: Hana Ku?erov?
body: Hi [@Octavio Ornelas](/t5/user/viewprofilepage/user-id/4660000) ,

would you please share the code with us, so we can help you with it? Thank you.

comments: author: Octavio Ornelas
body: 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


author: Hana Ku?erov?
body: Thank you [@Octavio Ornelas](/t5/user/viewprofilepage/user-id/4660000) . 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?


author: Octavio Ornelas
body: I tried using this <https://api.opsgenie.com/v2/schedules> i got the 422 error


author: Hana Ku?erov?
body: 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?


author: Octavio Ornelas
body: I changed the quotess and stil gives me the same response 422


author: Hana Ku?erov?
body: [@Octavio Ornelas](/t5/user/viewprofilepage/user-id/4660000) Are you able to get also error message what's wrong with the request body, not only error code?


author: Octavio Ornelas
body: 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}\]}}}


author: Hana Ku?erov?
body: 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.


author: Octavio Ornelas
body: 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>"


author: Hana Ku?erov?
body: Hi [@Octavio Ornelas](/t5/user/viewprofilepage/user-id/4660000) ,

according to the [documentation](https://docs.opsgenie.com/docs/schedule-api) you need to send PATCH request (not POST) to update the schedule.


author: Octavio Ornelas
body: 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


Tags: cloud