AlexW
AlexW

Reputation: 2587

Python /Json - KeyError 'Body' when creating

im getting the error

>>> appt = CreateEvent(authentication, result[0].calendarId, subject_text, start_time, end_time, subscribers, content)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 9, in CreateEvent
KeyError: 'Body'

for the below, code, im new to json so dont understand why this isnt working. all the others work but i see errors on the Body, is it because im using two []? this is to be sent to the office 365 API so it expects it in that format.

Thanks

def CreateEvent(auth, calendar, subject, start_time, end_time, attendees, content):
    create_url = 'https://outlook.office365.com/api/v1.0/me/calendars/{0}/events'.format(calendar)
    headers = {'Content-type': 'application/json', 'Accept': 'application/json'}
    data = {}
    data['Subject'] = subject
    data['Start'] = start_time
    data['End'] = end_time
    data['Attendees'] = attendees
    data['Body']['Content'] = content
    data['Body']['ContentType'] = 'Text'
    content_data = json.dumps(data)

Upvotes: 0

Views: 3998

Answers (1)

ettanany
ettanany

Reputation: 19816

You need first to have Body key in your data doctionary. You can solve your issue like this:

data = {'Body': {}}

data['Subject'] = subject
data['Start'] = start_time
data['End'] = end_time
data['Attendees'] = attendees
data['Body']['Content'] = content
data['Body']['ContentType'] = 'Text'
# ...

Upvotes: 3

Related Questions